JDBC——用Java在資料庫中建立一個Student表
阿新 • • 發佈:2018-12-04
一、Studnet表:
屬性:sno int(20)、cno int(4)、grade int(4)。即學號、課程號、成績。
二、Java中的類
用到的類: DBUtil(連線、關閉資料庫)、Student(學生資訊)、Userdemo(介面:實現Student表的增刪該查)、UserdemoImpl(實現Userdemo接口裡的方法)、Test(測試臺);
1、Student
//Student表 public class Student { private int sno; private int cno; private int grade; public Student(int sno, int cno, int grade) { this.sno = sno; this.cno = cno; this.grade = grade; } public Student() { } public String toString() { return this.sno+","+this.cno+","+this.grade; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public int getCno() { return cno; } public void setCno(int cno) { this.cno = cno; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } }
2、DBUtil
import java.io.*; import java.util.*; import java.sql.*; public class DBUtil { private static String driver = "com.mysql.cj.jdbc.Driver"; //用於載入驅動 private static String url = "jdbc:mysql://localhost:3306/sc3?serverTimezone=UTC"; //資料庫地址 private static String username = "root"; //使用者名稱 private static String password = "123456"; //密碼 //連線資料庫 public static Connection open() { try { Class.forName(driver); //載入驅動 return DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return null; } //關閉資料庫 public static void close(Connection conn) { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
3、Userdemo
import java.util.ArrayList;
//實現對資料庫中sc表的增、刪、改、查
public interface Userdemo
{
public void Insert(Student s);
public void Delete(int sno,int cno);
public void Update(Student s);
public Student Get(int sno,int cno);
public ArrayList<Student> SelectAll();
}
4、UserdemoImpl
import java.util.*;
import java.sql.*;
public class UserdemoImpl implements Userdemo //實現Userdemo介面的方法
{
@Override
public void Insert(Student s) //增
{
//連結資料庫
Connection conn = DBUtil.open();
//建立語句
String sql = "insert into sc values(?,?,?)";
try
{
//動態操作sql語句
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,s.getSno());
pstmt.setInt(2,s.getCno());
pstmt.setInt(3,s.getGrade());
pstmt.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn); //關閉資料庫
}
}
@Override
public void Delete(int sno, int cno) //刪
{
Connection conn = DBUtil.open();
String sql = "delete from sc where sno=? and cno=?";
try
{
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,sno);
pstmt.setInt(2,cno);
pstmt.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
}
@Override
public void Update(Student s) //改
{
Connection conn = DBUtil.open();
String sql = "update sc set grade=? where sno=? and cno=?";
try
{
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,s.getGrade());
pstmt.setInt(2,s.getSno());
pstmt.setInt(3,s.getCno());
pstmt.executeUpdate();
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
}
@Override
public Student Get(int sno, int cno) //查
{
Connection conn = DBUtil.open();
String sql = "select grade from sc where sno=? and cno=?";
try
{
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,sno);
pstmt.setInt(2,cno);
ResultSet rs = pstmt.executeQuery();
if(rs.next())
{
int grade = rs.getInt(1);
Student s = new Student(sno,cno,grade);
return s;
}
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
return null;
}
@Override
public ArrayList<Student> SelectAll() //遍歷
{
ArrayList<Student> stu = new ArrayList<Student>();
Connection conn = DBUtil.open();
String sql = "select * from sc";
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
int sno = rs.getInt(1);
int cno = rs.getInt(2);
int grade = rs.getInt(3);
Student s = new Student(sno,cno,grade);
stu.add(s);
}
return stu;
} catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
return null;
}
}
5、Test
import java.util.*;
import java.sql.*;
public class Test
{
public static void Create() //建立Student表
{
Connection conn = DBUtil.open(); //連線資料庫
String sql = "create table student(sno int(20),cno int(4),grade int(4),primary key(sno,cno))";
try
{
Statement stmt = conn.createStatement(); //靜態操作sql語句
stmt.execute(sql);
} catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
}
public static void show() //提示資訊
{
System.out.println("***********成績資料庫管理***********");
System.out.println("insert: 插入成績.");
System.out.println("delete: 刪除成績.");
System.out.println("update: 更新成績.");
System.out.println("get: 檢視成績.");
System.out.println("list: 檢視所有成績.");
System.out.println("***********成績資料庫管理***********");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Create();
Userdemo userdemo = new UserdemoImpl(); //多型
Student stu = new Student();
while(true)
{
show();
System.out.print("命令>");
String s = in.next();
if(s.equals("insert"))
{
System.out.println("請輸入正確的資訊,如果重複、不存在、資訊錯誤,都會出現錯誤.");
System.out.print("請輸入學號:");
int sno = in.nextInt();
System.out.print("請輸入課程號:");
int cno = in.nextInt();
System.out.print("請輸入成績:");
int grade = in.nextInt();
stu.setSno(sno);
stu.setCno(cno);
stu.setGrade(grade);
userdemo.Insert(stu);
}
else if(s.equals("delete"))
{
System.out.println("請輸入正確的資訊,如果重複、不存在、資訊錯誤,都會出現錯誤.");
System.out.print("請輸入待刪除的學號:");
int sno = in.nextInt();
System.out.print("請輸入待刪除的課程號:");
int cno = in.nextInt();
userdemo.Delete(sno, cno);
}
else if(s.equals("update"))
{
System.out.println("請輸入正確的資訊,如果重複、不存在、資訊錯誤,都會出現錯誤.");
System.out.print("請輸入待更新的學號:");
int sno = in.nextInt();
System.out.print("請輸入待更新的課程號:");
int cno = in.nextInt();
System.out.print("請輸入新成績:");
int grade = in.nextInt();
stu.setSno(sno);
stu.setCno(cno);
stu.setGrade(grade);
userdemo.Update(stu);
}
else if(s.equals("get"))
{
System.out.println("請輸入正確的資訊,如果重複、不存在、資訊錯誤,都會出現錯誤.");
System.out.print("請輸入查詢的學號:");
int sno = in.nextInt();
System.out.print("請輸入查詢的課程號:");
int cno = in.nextInt();
stu = userdemo.Get(sno, cno);
System.out.println("成績:"+stu.getGrade());
}
else if(s.equals("list"))
{
ArrayList<Student> note = userdemo.SelectAll();
for(int i=0,t=note.size();i<t;i++)
{
System.out.println(note.get(i));
}
}
else if(s.equals("exit"))
{
System.out.println("再見!");
System.exit(0);
}
}
}
}