一个完整的例子 - 通过 db 访问 Student 表并读取/打印记录的内容
前提: 已经在 mysql 中创建了一个叫做 student 的 schema, 在 schema 中创建了一个叫做 'student' 的表。
代码文件:
DBConnection.java
===========================
package com.Java程序设计入门.encapsulation;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.Connection;
import java.util.Iterator;
import java.util.Properties;
import com.sun.beans.util.Cache;
import com.sun.corba.se.impl.orbutil.GetPropertyAction;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class DBConnection {
private String url;//db url
private String userName;
private String password;
private String drivers;
public Connection getConnection()
{
Connection con = null;
getProperty();
try {
con = DriverManager.getConnection(url, userName, password);
}
catch(SQLException e) {
e.printStackTrace();
}
return con;
}
private void getProperty()
{
Properties properties = new Properties();
try {
File file = new File("D:\\EclipseSpaceJava程序入门设计\\practice\\src\\com\\Java程序设计入门\\encapsulation\\Driver.properties");
FileInputStream in = new FileInputStream(file);
properties.load(in);
this.drivers = properties.getProperty("drivers");
this.url = properties.getProperty("url");
this.userName = properties.getProperty("user");
this.password = properties.getProperty("password");
System.out.println("drivers: " + drivers);
System.out.println("url: " + url);
in.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
===========QueryTable.java=========== import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.Connection;
import java.util.Iterator;
import java.util.Properties;
import com.sun.beans.util.Cache;
import com.sun.corba.se.impl.orbutil.GetPropertyAction;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class DBConnection {
private String url;//db url
private String userName;
private String password;
private String drivers;
public Connection getConnection()
{
Connection con = null;
getProperty();
try {
con = DriverManager.getConnection(url, userName, password);
}
catch(SQLException e) {
e.printStackTrace();
}
return con;
}
private void getProperty()
{
Properties properties = new Properties();
try {
File file = new File("D:\\EclipseSpaceJava程序入门设计\\practice\\src\\com\\Java程序设计入门\\encapsulation\\Driver.properties");
FileInputStream in = new FileInputStream(file);
properties.load(in);
this.drivers = properties.getProperty("drivers");
this.url = properties.getProperty("url");
this.userName = properties.getProperty("user");
this.password = properties.getProperty("password");
System.out.println("drivers: " + drivers);
System.out.println("url: " + url);
in.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
package com.Java程序设计入门.encapsulation;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.util.Vector;
public class QueryTable {
/**
* 从表中查出所有记录
*/
public Vector getStudent(Connection con)
{
Vector vector = new Vector<>();
try {
Statement st = con.createStatement();
String sql = "select * from student";
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
String name = rs.getString("Name");
String number = rs.getString("Number");
String sex = rs.getString("Sex");
Date date = rs.getDate("Birthday");
String specility = rs.getString("Specility");
String address = rs.getString("Address");
Student one = new Student(name, number);
one.setSex(sex);
one.setSpeciality(specility);
one.setAddress(address);
one.setBirthday(date.toString());
vector.add(one);
}
st.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
return vector;
}
}
=============Driver.Properties============import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.util.Vector;
public class QueryTable {
/**
* 从表中查出所有记录
*/
public Vector getStudent(Connection con)
{
Vector vector = new Vector<>();
try {
Statement st = con.createStatement();
String sql = "select * from student";
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
String name = rs.getString("Name");
String number = rs.getString("Number");
String sex = rs.getString("Sex");
Date date = rs.getDate("Birthday");
String specility = rs.getString("Specility");
String address = rs.getString("Address");
Student one = new Student(name, number);
one.setSex(sex);
one.setSpeciality(specility);
one.setAddress(address);
one.setBirthday(date.toString());
vector.add(one);
}
st.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
return vector;
}
}
drivers=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student?user=root&password=root&useUnicode=true&characterEncoding=UTF8
DatabaseName=student
user=root
password=root
explanation=A db named 'student' with a table named 'stdent' are created in mysql db.
url=jdbc:mysql://localhost:3306/student?user=root&password=root&useUnicode=true&characterEncoding=UTF8
DatabaseName=student
user=root
password=root
explanation=A db named 'student' with a table named 'stdent' are created in mysql db.
=============TestStudentDB.java==============
package com.Java程序设计入门.encapsulation;
import java.sql.Connection;
import java.util.Vector;
public class TestStudentDB {
public static void main(String[] args) {
DBConnection dbConnection = new DBConnection();
Connection con = dbConnection.getConnection();
QueryTable queryTable = new QueryTable();
Vector vector = queryTable.getStudent(con);
System.out.println("Elements in Vector are: " + vector.size());
for (int i = 0; i < vector.size(); i++) {
System.out.println((Student)vector.get(i));
}
}
}
import java.sql.Connection;
import java.util.Vector;
public class TestStudentDB {
public static void main(String[] args) {
DBConnection dbConnection = new DBConnection();
Connection con = dbConnection.getConnection();
QueryTable queryTable = new QueryTable();
Vector vector = queryTable.getStudent(con);
System.out.println("Elements in Vector are: " + vector.size());
for (int i = 0; i < vector.size(); i++) {
System.out.println((Student)vector.get(i));
}
}
}