日期:2014-05-16 浏览次数:20815 次
1.加载MySql驱动程序
Class.forName("com.mysql.jdbc.Driver"); // 加载MySql驱动程序
2.建立Connection连接对象
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?user=root&password=root");
其中
url = "jdbc:mysql://localhost/";
serverName = "localhost";
portNumber = "3306";
databaseName = "my_db";
userName = "root";
password = "root";
3.建立Statement语句对象
Statement st = con.createment();
4.执行sql语句
st.executeUpdate(“SQL语句”);//插入
st.executeUpdate(“SQL语句”);//更新
st.executeexecuteQuery(“SQL语句”);//查询
5.关闭连接
con.close();
6.实现案例
DBConn.java中是对连接数据库的实现
package db; import java.sql.Connection; import java.sql.DriverManager; public class DBConn { /* 获取Conncetion对象并返回 */ public Connection getConnection() { Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); // 加载MySql驱动程序 //con = DriverManager.getConnection("jdbc:mysql://localhost/cloud_db?user=root&password=root&autoReconnect=true&useUnicode=true&characterEncoding=utf8"); con=DriverManager.getCo