1.数据库连接的五种方式
import com.mysql.jdbc.Driver;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Connection {
@Test
public void testConnection1() throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "abc123");
java.sql.Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
@Test
public void testConnection2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
Class aClass = Class.forName("com.mysql.jdbc.Driver");
java.sql.Driver driver = (java.sql.Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "abc123");
java.sql.Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
@Test
public void testConnection3() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
Class aClass = Class.forName("com.mysql.jdbc.Driver");
java.sql.Driver driver = (java.sql.Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "abc123");
DriverManager.registerDriver(driver);
java.sql.Connection connection = DriverManager.getConnection(url, properties);
System.out.println(connection);
}
@Test
public void testConnection4() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "abc123");
java.sql.Connection connection = DriverManager.getConnection(url, properties);
System.out.println(connection);
}
@Test
public void testConnection5() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, IOException {
InputStream resourceAsStream = Connection.class.getClassLoader().getResourceAsStream("sql.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
Class.forName(properties.getProperty("driverClass"));
java.sql.Connection connection = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("user"), properties.getProperty("password"));
System.out.println(connection);
}
}
2.使用preparedStatement进行增删改
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class InsertTest {
@Test
public void testInsert() {
InputStream resourceAsStream = Connection.class.getClassLoader().getResourceAsStream("sql.properties");
Properties properties = new Properties();
java.sql.Connection connection = null;
PreparedStatement preparedStatement = null;
try {
properties.load(resourceAsStream);
Class.forName(properties.getProperty("driverClass"));
connection = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("user"), properties.getProperty("password"));
String sqlString = "insert into customers(name,email,birth) values(?,?,?)";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setString(1, "张飞");
preparedStatement.setString(2, "zhangfei@163.com");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parseDate = simpleDateFormat.parse("1000-01-01");
preparedStatement.setDate(3, new java.sql.Date(parseDate.getTime()));
preparedStatement.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3.建立DBUtil工具类,并且实现一个修改操作
DBUtil工具类:
import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
public static java.sql.Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
InputStream resourceAsStream = Connection.class.getClassLoader().getResourceAsStream("sql.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
Class.forName(properties.getProperty("driverClass"));
java.sql.Connection connection = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("user"), properties.getProperty("password"));
System.out.println(connection);
return connection;
}
public static void closeResource(java.sql.Connection connection, Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
修改操作:
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class InsertTest {
@Test
public void testInsert() {
InputStream resourceAsStream = Connection.class.getClassLoader().getResourceAsStream("sql.properties");
Properties properties = new Properties();
java.sql.Connection connection = null;
PreparedStatement preparedStatement = null;
try {
properties.load(resourceAsStream);
Class.forName(properties.getProperty("driverClass"));
connection = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("user"), properties.getProperty("password"));
String sqlString = "insert into customers(name,email,birth) values(?,?,?)";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setString(1, "张飞");
preparedStatement.setString(2, "zhangfei@163.com");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parseDate = simpleDateFormat.parse("1000-01-01");
preparedStatement.setDate(3, new java.sql.Date(parseDate.getTime()));
preparedStatement.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testUpdate() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DBUtil.getConnection();
String sql = "update customers set name = ? where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, "莫扎特");
preparedStatement.setObject(2, 18);
preparedStatement.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
DBUtil.closeResource(connection, preparedStatement);
}
}
|