-----连接数据库
package connectdb;
import java.sql.*;
class Dbcon {
// 此处连接数据库,独立开一个类,以后操作数据库的每次连接就不用写这么多
public Connection getCon() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// 其中test是我们要链接的数据库,user是数据库用户名,password是数据库密码。
// 3306是mysql的端口号,一般是这个
// 后面那串长长的参数是为了防止乱码,免去每次都需要在任何语句都加入一条SET NAMES UTF8
String url = "jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8&useSSL=false";
String user = "root";
String password = "basketball";
con = DriverManager.getConnection(url, user, password);
System.out.println("sucess to connect db");
} catch (Exception e) {
System.out.println("fail to connect db");
e.printStackTrace();
}
return con;
}
}
增删改查操作:
package connectdb;
import java.sql.*;
import java.util.ArrayList;
public class applyDb {
//定义一个Student类,存到数据库数据类型是Student
class Student{
private String number;
private String name;
private String grade;
public Student(String num,String name,String grade){
this.number = num;
this.name = name;
this.grade = grade;
}
public String getNumber(){
return number;
}
public String getName(){
return name;
}
public String getGrade(){
return grade;
}
}
//定义一个ArrayList来存放数据
public ArrayList<Student> dataList(){
Student stu1 = new Student("1","李青","90");
Student stu2 = new Student("2","小明","60");
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
return stu;
}
//创建一个表
public void create(){
Connection con = new Dbcon().getCon();
try {
Statement s = con.createStatement();
String creatT = "create table student(number char(10),name char(15),grade char(15))";
s.executeUpdate(creatT);
s.close();
con.close();
System.out.println("sucess to create");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("fail to create");
e.printStackTrace();
}
}
//插入数据
public void insert(){
Connection con = new Dbcon().getCon();
ArrayList<Student> arStu = dataList();
int length = arStu.size();
String number = "";
String name = "";
String grade = "";
PreparedStatement ps = null;
try {
for(int i = 0 ;i<length;i++){
number = arStu.get(i).number;
name = arStu.get(i).name;
grade = arStu.get(i).grade;
String sql = "insert into student values(?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1,number);
ps.setString(2, name);
ps.setString(3, grade);
ps.executeUpdate();
}
ps.close();
con.close();
System.out.println("sucess to insert");
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println("fail to insert");
e1.printStackTrace();
}
}
//删除数据
public void delete(){
Connection con = new Dbcon().getCon();
String sql = "delete from student where number=?";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
ps.setString(1, "1");
ps.executeUpdate();
ps.close();
con.close();
System.out.println("sucess to delete");
} catch (SQLException e) {
System.out.println("fail to delete");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查找数据
public void query(){
Connection con = new Dbcon().getCon();
String sql = "select * from student where number=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps= con.prepareStatement(sql);
ps.setString(1,"2");
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("number"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("grade"));
}
ps.close();
con.close();
} catch (SQLException e) {
System.out.println("fail to query");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
applyDb db = new applyDb();
//db.create();
//db.insert();
//db.delete();
db.query();
}
}