java通过JDBC连接MySQL数据库实现 增删查(预编译方式)
准备工作: 软件myEclipse 数据库MySQL
MySQL的驱动下载地址:点击打开链接
JDBC(Java Data Base Connectivity,) java数据库连接是一种用于执行SQL语句的Java API
- 加载jdbc驱动,实现数据库连接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Data {
public static final String url="jdbc:mysql://localhost:3306/phone";
public static final String name="com.mysql.jdbc.Driver";
public static final String user="root";
public static final String password="root";
public Connection conn=null;
public PreparedStatement pst=null;
public PreparedStatement DBHelp(String sql){
try {
Class.forName(name);
conn=DriverManager.getConnection(url, user, password);
pst=conn.prepareStatement(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pst;
}
public void close(){
try {
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2. 编写增删查
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class operation {
public ResultSet rs=null;
public PreparedStatement ps=null;
Data data=new Data();
public void show(){
String sql="select * from phoneinfor";
try {
rs=data.DBHelp(sql).executeQuery();
System.out.println("ID"+"\t"+"name"+"\t"+"telephone"+"\t"+"type");
while (rs.next()) {
int uid=rs.getInt("Id");
String uname=rs.getString("name");
String tele=rs.getString("telephone");
String type=rs.getString("type");
System.out.println(uid+"\t"+uname+"\t"+tele+"\t"+type);
}
data.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(String name,String tele,String type){
String sql="insert into phoneinfor(name,telephone,type) values(?,?,?)";
try {
ps=data.DBHelp(sql);
ps.setString(1, name);
ps.setString(2, tele);
ps.setString(3, type);
int f=ps.executeUpdate();
if (f>0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败,请重新添加");
}
data.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(String name){
String sql="delete from phoneinfor where name =?";
try {
ps=data.DBHelp(sql);
ps.setString(1, name);
int f=ps.executeUpdate();
if (f>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败,你输入的姓名不存在或者是输入有误");
}
data.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3. 实现操作类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
operation op=new operation();
Scanner sc=new Scanner(System.in);
boolean goon=true;
while (goon) {
index();
System.out.print("请输入你的选择:");
int input=sc.nextInt();
switch (input) {
case 1: op.show(); break;
case 2:
System.out.print("请输入姓名:");
String name=sc.next();
System.out.print("请输入电话号:");
String tele=sc.next();
System.out.print("请输入类型:");
String type=sc.next();
op.add(name, tele, type);
break;
case 3:
System.out.print("请输入姓名:");
String uname=sc.next();
op.delete(uname);
break;
case 4: goon=false;
System.out.println("谢谢你的使用,欢迎下次使用!");
break;
default:
System.out.println("*****请你输入正确的选择*****");
break;
}
}
}
public static void index(){
String[] select={"1.展现所有信息","2.添加用户","3.删除用户","4.退出"};
System.out.println("************************************");
for (int i = 0; i < select.length; i++) {
System.out.println("\t"+select[i]);
}
}
}