一、读取数据库
package com.djx.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.sun.org.apache.regexp.internal.recompile;
public class JDBETest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sql="select * from tbl_user";
Connection conn=null;
Statement statement=null;
ResultSet set=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root", "1234");
statement=(Statement) conn.createStatement();
set=statement.executeQuery(sql);
while(set.next()){
System.out.print(set.getInt("id")+" ");
System.out.print(set.getString("name")+" ");
System.out.print(set.getString("password")+" ");
System.out.print(set.getString("email")+" ");
System.out.println();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
set.close();
statement.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二、对表进行增加,修改,删除操作
package com.djx.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.sun.org.apache.regexp.internal.recompile;
public class JDBETest {
public static Connection getConnection() {
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root","1234");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void insert() {
Connection conn=getConnection();
String sql="insert into tbl_user(name,password,email) values('Tom','1234','tom@163.com')";
int count;
try {
Statement statement=(Statement)conn.createStatement();
count = statement.executeUpdate(sql);
System.out.println("向用户表中插入"+count+"条记录");
statement.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void update() {
Connection conn=getConnection();
String sql="update tbl_user set email='tom@qq.com' where name='Tom'";
int count;
try {
Statement statement=(Statement)conn.createStatement();
count = statement.executeUpdate(sql);
System.out.println("用户表中更新"+count+"条记录");
statement.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delete() {
Connection conn=getConnection();
String sql="delete from tbl_user where name='Tom'";
int count;
try {
Statement statement=(Statement)conn.createStatement();
count = statement.executeUpdate(sql);
System.out.println("用户表中更新"+count+"条记录");
statement.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
insert();
update();
delete();
}
}