jdbc开发步骤

新建一个dbutil类

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {

private static final String URL="jdbc:mysql://120.55.190.163:3306/bdjf_organ";
private static final String USER="root";
private static final String PASSWORD="";

private static Connection conn=null;

static {
try {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static Connection getConnection(){
return conn;
}

}

如何调用dbutil


private void insertDate() throws SQLException {
//一个连接对象
PreparedStatement ps = null;
Connection conn = DBUtil.getConnection(); //得到一个连接
//用于插入数据
//sql语句,向表里插入数据

String sql = "insert into Content01(Content) values(?)";
ps = conn.prepareStatement(sql);
try {
ps.setString(1, Context);

ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

 

posted @ 2017-05-25 11:10  鱼長  阅读(173)  评论(0编辑  收藏  举报