生活随笔
收集整理的這篇文章主要介紹了
JDBC练习一
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC練習一
import java.sql.*;public class JDBCTest01 {public static void main(String[] args
) {Connection conn
= null;Statement stmt
= null;try{Driver driver
= new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver
);String url
= "jdbc:mysql://127.0.0.1:3306/mydb";String user
= "root";String password
= "123456";conn
= DriverManager.getConnection(url
,user
,password
);System.out
.println("數據庫連接對象" + conn
); stmt
= conn
.createStatement();int count
= stmt
.executeUpdate("insert into student values(5,'田七')");System.out
.println(count
== 1 ? "保存成功":"保存失敗");} catch(SQLException e
) {e
.printStackTrace();} finally {if(stmt
!= null) {try {stmt
.close(); }catch (SQLException e
) {e
.printStackTrace();}}if(conn
!= null) {try {conn
.close(); }catch (SQLException e
) {e
.printStackTrace();}}}}
}
運行結果:
總結
以上是生活随笔為你收集整理的JDBC练习一的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。