Until
import java.sql.*;
public class Util
{
public static Connection getConnection() throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.创建连接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/zhong","root","root");
System.out.println("ok");
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();
}
}
}
Find
import java.sql.*;
public class Find
{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
try
{
Util util=new Util();
connection=util.getConnection();
//3.写sql语句
String sql="select * from user";
//4.获得statement
ps=connection.prepareStatement(sql);
//5.执行sql,得到结果集
rs=ps.executeQuery();
//6.处理结果集
while (rs.next())
{
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3));
System.out.println(" ");
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
//7.关闭资源
Util.closeAll(rs,ps,connection);
}
}
}
Add
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Add
{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
try
{
//1.加载驱动
connection=Util.getConnection();
//3.写sql语句
String sql="insert into user(username,password) values ('dd','123')";
//4.获得statement
statement=connection.prepareStatement(sql);
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
//7.关闭资源
Util.closeAll(rs,statement,connection);
}
}
}
Delete
import java.sql.*;
public class Delete
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
try
{
//1.加载驱动
connection=Util.getConnection();
//3.写sql语句
String sql="delete from user where username='ddd'";
//4.获得statement
statement=connection.prepareStatement(sql);
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
//7.关闭资源
Util.closeAll(rs,statement,connection);
}
}
}
Update
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Update
{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
try
{
//1.加载驱动
connection=Util.getConnection();
//3.写sql语句
String sql="update user set username='ddd' where username='dd'";
//4.获得statement
statement=connection.prepareStatement(sql);
//5.执行sql,得到结果集
statement.executeUpdate();
//6.处理结果集
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
//7.关闭资源
Util.closeAll(rs,statement,connection);
}
}
}