代码示例
有两种加载mysql链接的方式
示例1
import java.sql.*;
public class demo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1,加载驱动
/*
* 常规加载驱动语法
* DriverManager.registerDriver(new com.mysql.jdbc.Driver());
*
* 推荐使用下面的固定写法,这个写法与 注解和反射 知识有关
*/
Class.forName("com.mysql.jdbc.Driver");
//2,用户信息和URL
/*
* useUnicode=true 使用中文编码
* characterEncoding=utf8 字符集使用UTF-8
* useSSL=true 使用安全的链接
* */
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root"; //数据库用户名
String password = ""; //数据库密码
//3,连接成功,返回一个数据库对象,
//连接成功,connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//4,执行SQL的对象,执行SQL的对象
Statement statement = connection.createStatement();
//5,执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果
String sql = "select * from test";
ResultSet resultSet = statement.executeQuery(sql); //查询操作,返回结果集 ResultSet
int rows = statement.executeUpdate(); //插入,更新,删除都可以用这个,返回一个受影响的行数
statement.execute(sql); //可以操作任何语句
int[] rowsArray = statement.executeBatch(sql1,sql2); //同时操作多个语句
while(resultSet.next()){
//getObject() 参数是字段名
System.out.println("id="+resultSet.getObject("id"));
System.out.println("id="+resultSet.getObject("name"));
System.out.println("id="+resultSet.getObject("password"));
}
//释放连接(关闭连接),先开后关原则
resultSet.close();
statement.close();
connection.close();
}
}
示例2
JdbcUtils.java
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
//src目录下的配置文件都可以通过反射来拿到
static{
try{
//获取类加载器,获取资源
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
//配置文件的信息现在就在 properties 对象里面了
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//驱动只用加载一次
Class.forName(driver);
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
c.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
//释放连接
public static void release(Connection conn, Statement st, ResultSet rs){
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st!=null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
JdbcTest.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//获取数据库连接
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "select * from test";
rs = st.executeQuery(sql);
/*
int i = st.executeUpdate(sql);
if (i>0){
System.out.println("操作成功");
}
*/
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root