操作mysql --crud:增删改查-- insert、delete、update、select
获取连接:1.准备sql: 1.insert、delete、update 2.select
2.执行sql: 1.insert、delete、update 2.select
DBUtil:工具类,通常都以util结尾
方法:
获取mysql的连接、关闭连接
加驱动:Class.forName(“com.mysql.jdbc.Driver”);(从第三方依赖包里下下来的)
操作mysql方法、查询数据方法
需要先准备sql连接:prepareStatement,再执行SQL:execute
/**
* 获取mysql 连接
* @param url
* @param user
* @param passwd
* @return
*/
public static Connection getConnection(String url, String user, String passwd){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
try {
connection = DriverManager.getConnection(url, user, passwd);
//建立连接
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
/**
* 关闭连接
* @param conn
*/
public static void closeConnection(Connection conn){
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 操作mysql
* @param conn
* @param sql
*/
public static void exeSQL(Connection conn,String sql){
System.out.println("sql=>:"+sql);
//加一个反应,看sql语句运行到哪里
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 查询数据
* @param conn
* @param sql
*/
//void→返回值类型为空,如果有返回值,则要把void换成返回的值的类型
public static ResultSet selectSQL(Connection conn,String sql){
System.out.println("sql=>:"+sql);
ResultSet resultSet=null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
resultSet=pstmt.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return resultSet;
}
JDBCAPP:主代码
public static void main(String[] args) {
String url = "jdbc:mysql://ip:3306/库名"; //sql地址
String user="root"; //sql用户
String passwd = "123456"; //sql密码
//1.获取mysql的连接
Connection connection = DBUtils.getConnection(url, user, passwd);
//2.操作mysql
//1.insert delete update
//可以使用dbeaver快速写语法:选中你要更改的数据库下的表,右键,选择你要操作的语法,copy过来,然后更改里面的字段
//**注意copy时不要加分号
//sql语句放到java里把双引号换成单引号""→'',防止符号冲突
// String sql="INSERT INTO bigdata.emp\n" +
// "(empno, ename, job, mgr, hiredate, sal, comm, deptno)\n" +
// "VALUES(1234, 'zs', 'sale', 5678, current_timestamp, 100, 100, 1);\n";
// DBUtils.exeSQL(connection,sql);
// String sql ="UPDATE bigdata.emp\n" +
// "SET ename='lebulang' where ename='JAMES'\n";
// DBUtils.exeSQL(connection,sql); //调用工具类下的方法
//2.select查询
String sql="SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno\n" +
"FROM bigdata.emp";
ResultSet resultSet = DBUtils.selectSQL(connection, sql); //调用工具类下的方法(这个方法里有返回值),返回结果
try {
while(resultSet.next()){ //迭代器:判断读取的下一行数据是否为空,若为空则跳出
int empno = resultSet.getInt(1);
String ename = resultSet.getString(2);
String job = resultSet.getString(3);
int mgr = resultSet.getInt(4);
Date hiredate = resultSet.getDate(5);
int sal = resultSet.getInt(6);
int comm = resultSet.getInt(7);
int deptno = resultSet.getInt(8);
//可以全用String类型,可以全部转换成string类型输出
System.out.println(empno+","+ename+","+job+","+mgr+","+hiredate+","+sal+","+comm+","+deptno);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
//3.关闭连接
DBUtils.closeConnection(connection);
}

550

被折叠的 条评论
为什么被折叠?



