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

------解决思路----------------------
调试看看。 ,