sx
的数据库,在sx
数据库中有一张tb_user
的表tb_user
的表中有id
,username
,password
三个字段语法:
INSERT INTO [TABLE_NAME] (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
例子:
INSERT INTO tb_user VALUE (2,'test','123456')
语法:
DELETE FROM [table_name]
WHERE [condition];
例子:
DELETE FROM tb_user WHERE id=2;
语法:
UPDATE [table_name]
SET column1 = value1, column2 = value2...., columnN = valueN
例子:
UPDATE tb_user SET password='123456' WHERE id=2
语法:
SELECT column1, column2, columnN FROM table_name;
例子:
SELECT * FROM tb_user
Class.forName("指定数据库的驱动程序")
加载驱动。Class.forName("com.mysql.jdbc.Driver");
DriverManager
类的getConnectin
方法传入指定的欲连接数据库路径,数据库用户名和数据库密码来获得Connection
对象。一般格式为Connection connection = DriverManager.geiConnection(“连接数据库的URL", "数据库用户名", "数据库密码”);
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/sx","root","");
Statement
和PreparedStatement
。区别是:
执行静态SQL语句。通常通过
Statement
实例实现。 后面SQL语句在executeUpdate()
方法中写入。
Statement statement= connection.createStatement() ;
执行动态SQL语句。通常通过
PreparedStatement
实例实现。prepareStatement
方法中预先存入SQL语句。但是执行SQL语句还是executeUpdate
方法
PreparedStatement statement=connection.prepareStatement(sql);
statement.executeUpdate();
resultSet.close();
statement.close();
connection.close();
import java.sql.*;
public class Find {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
/*加载驱动*/
Class.forName("com.mysql.jdbc.Driver");
/*创建连接*/
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/sx","root","");
System.out.println("创建连接成功");
/*写sql语句*/
String sql="SELECT * FROM tb_user";
/*获得statement对象*/
PreparedStatement statement=connection.prepareStatement(sql);
/*执行sql 得到结果集*/
ResultSet resultSet=statement.executeQuery();
/*处理结果集*/
while (resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
/*关闭资源*/
resultSet.close();
statement.close();
connection.close();
}
}
案例结构图
因为我们增删改查都要用到加载驱动,连接数据库,关闭资源
所以我们就把加载驱动,连接数据库,关闭资源这三个模块集成到一起
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
/*加载驱动*/
Class.forName("com.mysql.jdbc.Driver");
/*创建连接*/
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/sx","root","");
System.out.println("创建连接成功");
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet!=null){
resultSet.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
System.out.println("所有连接关闭成功");
}
}
import DBUtil.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Add {
public static void main(String[] args){
Connection connection= null;
PreparedStatement statement= null;
try {
connection = DBUtil.getConnection();
String sql="INSERT INTO tb_user VALUE (2,'test','123456')";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
import DBUtil.DBUtil;
import java.sql.*;
public class Delete {
public static void main(String[] args){
Connection connection= null;
PreparedStatement statement= null;
try {
connection = DBUtil.getConnection();
String sql="DELETE FROM tb_user WHERE id=2;";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
import DBUtil.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Update {
public static void main(String[] args){
Connection connection= null;
PreparedStatement statement= null;
try {
connection = DBUtil.getConnection();
String sql="UPDATE tb_user SET password='123456' WHERE id=2";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
import DBUtil.DBUtil;
import java.sql.*;
public class Find {
public static void main(String[] args) {
Connection connection= null;
PreparedStatement statement= null;
ResultSet resultSet=null;
try {
connection = DBUtil.getConnection();
String sql="SELECT * FROM tb_user";
statement = connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
DBUtil.closeAll(resultSet,statement,connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}