首页 > 经验记录 > JDBC基本语法

JDBC基本语法

简单的JDBC应用:

Connection c = null;
Statement s = null;
try {
  //Class.forName是把这个类加载到JVM中,加载的时候,就会执行其中的静态初始化块,完成驱动的初始化的相关工作。
Class.forName(“com.mysql.jdbc.Driver”);
 /* 建立与数据库的Connection连接 这里需要提供:
  数据库所处于的ip:127.0.0.1 (本机) 数据库的端口号: 3306 (mysql专用端口号) 以及:数据库名称、编码方式、账号 、密码 */
  c = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/mybase?characterEncoding=UTF-8″ ,”root”,”614″);
  System.out.println(“连接成功,获取连接对象”+c);
  //Statement是用于执行SQL语句的
  s = c.createStatement();

 
String sql =”update product set pname=’高达’ , price=98000400 where id=10″;//sql语句,往execute里直接塞就是了
s.execute(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// 数据库的连接时有限资源,相关操作结束后,养成关闭数据库的好习惯
// 先关闭Statement
if (s != null)
try {
s.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 后关闭Connection
if (c != null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

           


CAPTCHAis initialing...
EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00