1、jdbc连接数据库
加l连求执释:
加载驱动 Class.forname(“commy.sql.jdbc.driver”);
创建连接对象:DriverManager.getConnecton("jdbc:mysql:///"+数据库名称,username,passw);//连接本机数据库
创建SQL字符串,请求执行相关语据:String sql="执行增删改语句“”;
执行SQL:
释放空间
2、jdbc管理事务
/** * jdbc连接本机数据库工具类 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtil {
/* public static void main(String[] args) {
}*/
private static String url;//访问地址
private static String user;//当前用户名
private static String pwd;//当前数据库密码
private static String driver;
static {
//初始化数据
url = "jdbc:mysql://localhost:3306/myscholl2";
user = "root";
pwd = "root";
driver = "com.mysql.jdbc.Driver";
// System.out.println(url+user+pwd+driver);
}
/**
* 获取Connection
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 增删改
*
* @param connection 连接对象
* @param sql 执行相关操作的mysql语句
* @param objects 任意类参数
* @return
*/
public static int addDeleteUpdate(Connection connection, String sql, Object... objects) throws SQLException {
int count = 0;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int index = 1;
if (objects != null) {
for (Object o : objects
) {
preparedStatement.setObject(index, o);
index++;
}
}
count = preparedStatement.executeUpdate();
return count;
}
/**
* 查询
*
* @param connection
* @param sql
* @param objects
* @return
* @throws SQLException
*/
public static ResultSet query(Connection connection, String sql, Object... objects) throws SQLException {
ResultSet resultSet = null;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int index = 1;
if (objects != null) {
for (Object o : objects
) {
preparedStatement.setObject(index, o);
index++;
}
}
resultSet = preparedStatement.executeQuery();
return resultSet;
}
/**
* 释放空间
*/
public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
if (preparedStatement != null) {
preparedStatement.close();
}
if (resultSet != null) {
resultSet.close();
}
if (connection != null)
connection.close();
}}
public class UtileTest {
/*
private static String url;//访问地址
private static String user;//当前用户名
private static String pwd;//当前数据库密码
private static String driver;
static {
//初始化数据
url = "jdbc:mysql://localhost:3306/myscholl2";
user = "root";
pwd = "root";
driver = "com.mysql.jdbc.Driver";
// System.out.println(url+user+pwd+driver);
}*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//获得Connection对象
// Class.forName(driver);
Connection updatedatabase =JDBCUtil.getConnection();
//开启事务
updatedatabase.setAutoCommit(false);
//执行相关操作
int i=JDBCUtil.addDeleteUpdate(updatedatabase,"update emp set sal=? where empno=?",12000,7698);
System.out.println(i);
updatedatabase.commit();
//释放空间
JDBCUtil.close(updatedatabase,null,null);
}
}