用Java代码操作数据库,可以用JDBC。首先要加载驱动,之后建立连接,再然后就可以对数据库进行操作。
1.建立连接。此处用的是MySQL数据库
public class DBUtil { public static String URL = "jdbc:mysql://localhost:3306/JDBCDEmo";
public static String USER = "root";
public static String PASSWORD = "abc123_"; private static Connection conn; static{
try {
//或得去驱动
Class.forName("com.mysql.jdbc.Driver");
//得到数据库的连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static Connection getConnection(){
return conn;
} }
2. 建立连接以后,就可以用相应的方法就行CRUD了public class BoyDao {
public void addBoy(Boy by) {
//或得连接,编写sql语句,?号代表要插入额数据
Connection conn = DBUtil.getConnection();
String sql = "INSERT INTO Boy" + " (user_name, sex, age, birthday, email, mobile, create_user, "
+ "create_date, update_user, update_date, isdel)"
+ " values(?, ?, ?, ?, ?, ?, ?, current_date(), ?, current_date(), ?)";
try {
1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理
关于是使用PrepareStatement还是普通的statement 请参考这里 http://www.cnblogs.com/0banana0/articles/2029863.html
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, by.getUser_name());
pst.setInt(2, by.getSex());
pst.setInt(3, by.getAge());
pst.setDate(4, new Date(by.getBirthday().getTime()));
pst.setString(5, by.getEmail());
pst.setString(6, by.getMobile());
pst.setString(7, by.getCreate_user());
pst.setString(8, by.getUpdate_user());
pst.setInt(9, by.getIsdel());
pst.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//修改
public void updateBoy(Boy by){
Connection conn = DBUtil.getConnection();
String sql = "Update Boy set " + " user_name = ?, sex = ?, age = ?, birthday = ?, email = ?, mobile = ?, create_user = ?, "
+ "create_date = current_date(), update_user = ?, update_date = current_date(), isdel = ?"
+ " where id = ?";
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, by.getUser_name());
pst.setInt(2, by.getSex());
pst.setInt(3, by.getAge());
pst.setDate(4, new Date(by.getBirthday().getTime()));
pst.setString(5, by.getEmail());
pst.setString(6, by.getMobile());
pst.setString(7, by.getCreate_user());
pst.setString(8, by.getUpdate_user());
pst.setInt(9, by.getIsdel());
pst.setInt(10, by.getId());
pst.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void deleteBoy(Boy by) throws Exception{
Connection conn = DBUtil.getConnection();
String sql ="DELETE FROM Boy" + " where id = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, by.getId());
pst.execute();
} public List<Boy> queray() {
List<Boy> list = new ArrayList<>();
try {
Connection conn = DBUtil.getConnection();
Statement sm = conn.createStatement();
//需要获得 结果操作集,之后就可以得到查询出来的结果 .next()是判断有没有结果,并把操作集的指针移到下一个操作集上。其实查询出来的结果可以看做是数据库中的一行一行的数据,那么刚开始的时候指针指向数据表的上一行,好比一个空的头指针,所以要移到下一个,以判断到底有没有数据。
ResultSet rs = sm.executeQuery("SELECT user_name FROM Boy");
while (rs.next()) {
String name = rs.getString("user_name");
Boy by = new Boy();
by.setUser_name(name);
list.add(by);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return list; }
}