注意:
你可以下载本站提供的 jar 包:mysql-connector-java-5.1.39-bin.jar(http://static.runoob.com/download/mysql-connector-java-5.1.39-bin.jar)
一. 将jar包导入到WEB-INF/lib目录下,
使用Java设置数据库连接
package www.yiqing.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.jasper.tagplugins.jstl.core.Out;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class JDBCTest {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db", "root", "gjf");
} catch (Exception e) {
// TODO: handle exception
}
return connection;
}
public static void insertUsertbl(Connection connection) throws SQLException {
String sql = "INSERT INTO tbl_user(id, name, password, email) VALUES(10, 'kk', 7777, 888@qq.com)";
Statement statement = (Statement) connection.createStatement();
statement.executeQuery(sql);
}
public static void insertAddresstbl(Connection connection) throws SQLException {
String sql = "INSERT INTO tbl_addresss(id, city, country, user_id) VALUES(3, 'shanghai', 'jingan', 10)";
Statement statement = (Statement) connection.createStatement();
statement.executeQuery(sql);
}
public static void main(String[] args) throws SQLException {
Connection connection = null;
try {
connection = getConnection();
connection.setAutoCommit(false);
insertUsertbl(connection);
insertAddresstbl(connection);
connection.commit(); // 提交事务
} catch (Exception e) {
// TODO: handle exception
System.out.println("============捕获到异常信息============");
e.printStackTrace();
try {
connection.rollback(); // 事务回滚
System.out.println("==事务回滚==");
} catch (Exception e2) {
// TODO: handle exception
}finally {
try {
if(connection != null){
connection.close();
}
} catch (Exception e3) {
// TODO: handle exception
}
}
}
}
}
使用配置文件进行数据库连接
5.建立连接数据库的工厂类
package www.yiqing.util;
import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;
import com.mysql.jdbc.Connection;
public class ConnectionFatory {
private static String driver;
private static String dburl;
private static String user;
private static String password;
private static final ConnectionFatory connectionFatory = new ConnectionFatory();
private Connection connection;
static {
Properties properties = new Properties();
try {
InputStream input = ConnectionFatory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
properties.load(input);
} catch (Exception e) {
// TODO: handle exception
System.out.println("配置文件读取失败!!!");
}
driver = properties.getProperty("driver");
dburl = properties.getProperty("dburl");
user = properties.getProperty("user");
password = properties.getProperty("password");
}
private ConnectionFatory() {
}
public static ConnectionFatory getInstance() {
return connectionFatory;
}
public Connection makeConnection() {
try {
Class.forName(driver);
connection = (Connection)DriverManager.getConnection(dburl, user, password);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return connection;
}
}
6.接下来就是使用了(新建一个类)
package www.yiqing.test;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import www.yiqing.util.ConnectionFatory;
public class ConnectionFactoryTest {
public static void main(String[] args) throws SQLException {
ConnectionFatory connectionFatory = ConnectionFatory.getInstance();
Connection connection = connectionFatory.makeConnection();
System.out.println(connection.getAutoCommit());
}
}