connectdatabase.java~29~
来自「java(Swing) access做的成绩管理系统」· JAVA~29~ 代码 · 共 65 行
JAVA~29~
65 行
package edu.xscj.conn;
import java.sql.*;
public class ConnectDataBase {
public ConnectDataBase() {
}
public static Connection getConn() {
Connection conn = null;
String strUrl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\jbuilder\\xscj\\database\\xscj.mdb";
// String strUrl1 = "jdbc:odbc:xscj"; //xscj为数据源
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(strUrl);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
ConnectDataBase connectdatabase = new ConnectDataBase();
Connection con = connectdatabase.getConn();
Statement stmt = null;
ResultSet rst = null;
String sql = "select * from student";
try {
stmt = con.createStatement();
rst = stmt.executeQuery(sql);
while (rst != null && rst.next()) {
System.out.println(rst.getString(1));
System.out.println(rst.getString(2));
System.out.println(rst.getString(3));
System.out.println(rst.getDate(4));
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (rst != null) {
try {
rst.close();
} catch (SQLException ex1) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex3) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex2) {
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?