//查看
/*String tableName=""; //表名
String sql="select * from "+tableName;
//连接数据库获取connection(一般单独写在一个工具类)
Connection conn = JdbcMysql.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){ //查询所有
rs.getString("name");
}
rs.close();*/
//插入
/*String sql ="insert into idandpassword (stuid,password) values(?,?)";//或者在问号处可直接写入新数据
//连接数据库获取connection(一般单独写在一个工具类)
Connection conn = JdbcMysql.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "iddd");//数字1插入的信息对应上面的问号位置
stmt.setString(2, "123");//插入的信息
stmt.executeUpdate();
stmt.close();*/
//查询
/*String sql = " SELECT * FROM images WHERE stuid = "+ stuid;
//连接数据库获取connection(一般单独写在一个工具类)
Connection conn = JdbcMysql.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
rs.next();
rs.getString("name");
rs.close();
*/
//修改
/*
String sql ="update idandpassword set password = ?, test = ? where stuid = ?";
//连接数据库获取connection(一般单独写在一个工具类)
Connection conn = JdbcMysql.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,"aaaaa");//方法中1,2,3对应问号位置
stmt.setInt(2, 122);
stmt.setString(3,"zzz");
stmt.executeUpdate();
stmt.close();
*/
//删除
/*String sql ="delete from idandpassword where stuid= ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,"ddd" ); //删除id="ddd"的数据
stmt.executeUpdate();
stmt.close();*/
关于关闭数据库连接:
最好是在工具类写一个方法把conn,stmt,rs都关闭掉。
JdbcMysql.closeAll(conn, stmt, rs);
|
|