java向SQLserver插入資料時提示‘‘沒有返回結果集‘‘
阿新 • • 發佈:2021-01-06
技術標籤:SQLJava-幫助小白Java學習分享sqlserver
public static Connection getConnection() {//配置資料庫資訊
String url="jdbc:sqlserver://localhost:1433;DatabaseName=abc";//資料庫地址
String username="user01";//資料庫使用者名稱
String password="123456";//資料庫密碼
Connection conn=null;
if (null==conn) {
try {
//載入驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//建立資料庫連結
conn =DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
}
return conn;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss" );
System.out.println("當前時間為: " + ft.format(dNow));
String sql="SET NOCOUNT ON; INSERT INTO ab(a,b) VALUES('"+ft.format(dNow)+ "','00:00:00')";
Connection conn=getConnection();
PreparedStatement pst=null;
try {
pst=conn.prepareStatement(sql);
pst.executeUpdate();//這裡有三個executeUpdate用於增刪改executeQuery用於查詢,execute通用所有增刪查改
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
```