当前位置: 代码迷 >> Java Web开发 >> 数据库批改语句
  详细解决方案

数据库批改语句

热度:71   发布时间:2016-04-14 09:02:11.0
数据库修改语句
package shujuku;
import java.sql.*;
public class Jdbcodbc {
public static void main(String [] args){
try{
//加载驱动  连接数据库第一步
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//连接数据路
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TEST", "sa", "123456");
String sql="upadte userTable set username=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,"zhangpengfe");
pstmt.setInt(2, 22);
int i=0;
i=pstmt.executeUpdate();
if(i!=0){
System.out.println("修改成功");
    }
pstmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}

}

------解决思路----------------------
没看到有什么明显的问题,不过代码写的很不规范。
操作数据库连接或者io之类的时候。应该使用try、catch、finally标准格式


Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=TEST", "sa",
"123456");

//to do something
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (Exception e) {
}
}

------解决思路----------------------
upadte userTable set username=? where id=?
其他没看,这里单词拼错了。。ad->da
------解决思路----------------------
调试看看。 ,
  相关解决方案