生活随笔
收集整理的这篇文章主要介绍了
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练习一的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。