通過java對資料庫的增刪查修操作
阿新 • • 發佈:2018-12-04
一 準備工作:
JDBC(Java Data Base Connectivity,java資料庫連線)是一種用於執行SQL語句的Java API,可以為多種關係資料庫提供統一訪問,它由一組用Java語言編寫的類和介面組成。JDBC提供了一種基準,據此可以構建更高階的工具和介面,使資料庫開發人員能夠編寫資料庫應用程式
首先要安裝好 【Mysql資料庫】,【SQLyog】,【Eclipse】。
SQLyog:其作用是連線資料庫對資料庫操作的圖形化介面。 通過SQLyog 在資料庫中新建一個名為st的資料庫並在下面建立一個名為student的表。二,具體實現
2.1 在eclipse中新建一個工程名為 jdbc 在該工程下滑鼠右鍵建立一個資料夾用來存放即將匯入的mysql-connector-java-5.0.3-bin.jar包。然後直接將mysql-connector-java-5.0.3-bin.jar包複製到該資料夾下。右鍵滑鼠鍵找到build path然後點選第一個選項將包匯入。
2.2 新建一個包,在該包下編寫增刪查修四個java的程式。
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Mod {
private final static String url = "jdbc:mysql://localhost:3306/db_students";
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//載入SQL驅動
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}//建立連線
public static void close(ResultSet rs, Statement st, Connection conn)
{
try
{
if(rs != null)
{
rs.close();
}
if(st != null)
{
st.close();
}
if(conn != null)
{
conn.close();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void close(Statement st, Connection conn)
{
close(null,st,conn);
}
}
, 2,SearchById : import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import cn.edu.hpu.jdbc1.Mod;
import cn.edu.hpu.jdbc1.Students;
public class SearchById {
public static void main(String[] args) {
int i = 1;
System.out.println(GetList(i).getId());
System.out.println(GetList(i).getName());
System.out.println(GetList(i).getTel());
}
public static Students GetList(int id){
Students Stu = new Students();
Connection C =null;
Statement S=null;
ResultSet R =null;
String sql = "select * from student where id ="+id;
C = Mod.getConnection();
try {
S = C.createStatement();
R = S.executeQuery(sql);
if(R.next()){
Stu.setId(R.getInt("id"));
Stu.setName(R.getString("name"));
Stu.setTel(R.getString("tel"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
Mod.close(R, S, C);
}
return Stu;
}
} 3 Student類: public class Students {
private int id;
private String name ;
private String tel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
} 4 插入操作: public class AddTest {
private final static String driver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost:3306/db_students";
public static void main(String[] args) throws SQLException {
Connection C = null;
// Jdbc1 D = new Jdbc1();
// D.
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
C = DriverManager.getConnection(url);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "insert into students(name,tel) values(?,?)";
PreparedStatement P = C.prepareStatement(sql);
P.setString(1, "bendan ");
P.setString(3, "shagua");
int rows = P.executeUpdate();
if(rows>0){
System.out.println("wancheng");
}
else
System.out.println("charushibai");
System.out.println("操作完成");
}
} 5 刪除操作: public class DelTest {
public static void main(String[] args) {
del(2);
}
public static boolean del(int id){
Mod M = new Mod();
Connection C = null;
PreparedStatement P = null;
boolean flag = false;
C = M.getConnection();
String St= "delete from student where id=?";
try {
P = C.prepareStatement(St);
P.setInt(1, id);
int rows = P.executeUpdate();
if(rows>0)
flag =true;
else
flag = false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
6,查詢操作: public class SearchById {
public static void main(String[] args) {
int i = 1;
System.out.println(GetList(i).getId());
System.out.println(GetList(i).getName());
System.out.println(GetList(i).getTel());
}
public static Students GetList(int id){
Students Stu = new Students();
Connection C =null;
Statement S=null;
ResultSet R =null;
String sql = "select * from student where id ="+id;
C = Mod.getConnection();
try {
S = C.createStatement();
R = S.executeQuery(sql);
if(R.next()){
Stu.setId(R.getInt("id"));
Stu.setName(R.getString("name"));
Stu.setTel(R.getString("tel"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
Mod.close(R, S, C);
}
return Stu;
}
}
7 更新操作:
public class UpdateTest {
public static void main(String[] args) {
SearchById Get = new SearchById();
Students Stu = Get.GetList(5);
Stu.setName("xiaozhu");
Stu.setTel("212");
if(Update(Stu))
System.out.println("更新成功");
else
System.out.println("更新失敗");
}
public static boolean Update(Students Stu){
Mod M = new Mod();
boolean flag = false;
// Connection C =null;
// PreparedStatement P = null;
// String sql="update student set name=? tel=? where id=?";
// C = M.getConnection();
try {
Connection C =null;
PreparedStatement P = null;
String sql="update student set name=?,tel=? where id=?";
C = M.getConnection();
P = C.prepareStatement(sql);
P.setString(1, Stu.getName());
P.setString(2, Stu.getTel());
P.setInt(3, Stu.getId());
int rows = P.executeUpdate();
if(rows>0){
flag =true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
M.close(P, C);
}
return flag;
}
}