一起无废话,看懂代码就行!
package baidu.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库CURD操作
* @author Administrator
*
*/
public class jdbcDemo {
//数据库地址
public final static String URL = "jdbc:mysql://localhost:3306/jdbc?Unicode=true&characterEncoding=UTF-8";
//数据库用户名
public final static String USER = "root";
//数据库密码
public final static String PWD = "root";
/**
* 掺入操作--C
*/
public static void insert(){
try {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、链接数据库
Connection conn = DriverManager.getConnection(URL, USER, PWD);
//3、构造SQL语句
String sql = "insert into pre_user (username,password,age) VALUE ('王武',88888,40)";
//4、构造Statement实例(发送sql语句的载体)
Statement stat = conn.createStatement();
//5、执行sql语句
//除了读取的sql语句,其他sql语句都叫更新executeUpdate
stat.executeUpdate(sql);
//6、关闭链接/释放资源
stat.close();
conn.close();
System.out.print("success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 更新操作
*/
public static void update(){
try {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、链接数据库
Connection conn = DriverManager.getConnection(URL, USER, PWD);
//3、构造SQL语句
String sql = "update pre_user set age = 88 where id = 4";
//4、构造Statement实例(发送sql语句的载体)
Statement stat = conn.createStatement();
//5、执行sql语句
//除了读取的sql语句,其他sql语句都叫更新executeUpdate
stat.executeUpdate(sql);
//6、关闭链接/释放资源
stat.close();
conn.close();
System.out.print("success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 删除操作
*/
public static void delete(){
try {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、链接数据库
Connection conn = DriverManager.getConnection(URL, USER, PWD);
//3、构造SQL语句
String sql = "delete from pre_user where id = 4";
//4、构造Statement实例(发送sql语句的载体)
Statement stat = conn.createStatement();
//5、执行sql语句
//除了读取的sql语句,其他sql语句都叫更新executeUpdate
stat.executeUpdate(sql);
//6、关闭链接/释放资源
stat.close();
conn.close();
System.out.print("success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查询操作
*/
public static void query(){
try {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、链接数据库
Connection conn = DriverManager.getConnection(URL, USER, PWD);
//3、构造SQL语句
String sql = "select id,username,age from pre_user";
//4、构造Statement实例(发送sql语句的载体)
Statement stat = conn.createStatement();
//5、执行sql语句
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("username");
int age = rs.getInt("age");
System.out.println("id = "+id+",username = "+name+",age ="+age);
}
//6、关闭链接/释放资源
rs.close();
stat.close();
conn.close();
System.out.print("success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 程序入口
* @param args
*/
public static void main(String[] args) {
//insert();
//update();
//delete();
query();
}
}
代码比较乱,望海涵!!!!