使用java连接Mysql数据库 2

来源:互联网 发布:国际志愿者网络 编辑:程序博客网 时间:2024/05/21 14:43

当建立数据库连接后(连接方式在:http://blog.csdn.net/dannor2010/article/details/8261382),我们需要对数据库表进操作。

Connection con = DButil.connect();//使用在上一个文章中的DButil 类进行数据库连接String sql1 = "insert into user(_name,_password,_email,_time) values(?,?,?,?)";//定义数据库查询语句PreparedStatement ps = con.prepareStatement(sql1);//prepareStatement 相对安全,数据操作效率高ps.setString(1, u.getName());//序列顺序为问号的顺序ps.setString(2, u.getPassword());ps.setString(3, u.getEmail());ps.setString(4, MyDate.getDateCN());ps.executeUpdate();//直接对数据库操作


数据库操作关键字:

PreparedStatementprepareStatement(sql1)ps.setStringexecuteUpdate()getPropertyload(is)class.getClassLoader().getResourceAsStream("DB.properties")DriverManager.getConnection(url, userName, password)



原创粉丝点击