import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQL {
private static final String name = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1:3306/";
//数据库名称
private static final String database = "book";
//用户名及密码
private static final String user = "root";
private static final String password = "";
protected Connection connect = null;
protected Statement statement = null;
{
try {
// 加载MySQL驱动
Class.forName(name);
// 通过DriverManger获取数据库连接
connect = DriverManager.getConnection(url + database, user, password);
// 通过Connection对象创建Statement对象
statement = connect.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 执行SQL查询语句
public ResultSet sqlQuery(String select) {
ResultSet res = null;
try {
res = statement.executeQuery(select);
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
// 执行SQL更新语句
public void sqlUpdate(String update) {
try {
statement.executeUpdate(update);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭连接
public void sqlClose() {
try {
statement.close();
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
MySQL驱动下载地址:http://dev.mysql.com/downloads/connector/j/
点击查看更多内容
15人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦