You are on page 1of 17

T T J N T T Load Driver)

N JDBC (Java Database Conectivity).

JDBC

Connection String Establish a database connection)


Create and execute an SQL Statement) Process the results) Close the database connection)

Load Driver)
JDBC C

forName

Class

public static Class forName(String className) throws ClassNotFoundException JDBC ODBC-JDBC Bridge

G Class.forName jdbc:odbc:JdbcOdbcDriver ;

Ghi -

C T

jdbc:odbc:JdbcOdbcDriver JDBC JDBC SQL S

Class.forName com.microsoft.sqlserver.jdbc.SQLServerDriver ; T Server com.microsoft.sqlserver.jdbc.SQLServerDriver JDBC SQL

T
C J

Establish a database connection)


D N M C C

SQLE

public static Connection getConnection(String strConnect) throws SQLException

H JDBC C

SQL S

String

strConnect

= "jdbc:odbc:driver={SQL Server};Server=BODUAD778EAB2E; Database =qlCuocHen; UserName=sa; Password=12345 6";

ConnectioncnObj = DriverManager.getConnection(strConnect); C

jdbc:odbc:driver={SQL Server};Server=<ServerName>; Database=<YourDB>;UserName=<dbUser>;Password=<** *>


N T > JDBC BRIDGE S B S

dbc:sqlserver://<server_name>:<port>;databaseName=<YourDB>;user= <userName>; passwor d=<pass>" B S

T Statement)
Khi 1 thng qua Statement PreparedStatement CallableStatement (T N C public Statement createStatement() throws SQLException VD SQL D

SQL Create and Execute an SQL

S
Statement Connection

createStatement

Connection ketNoi = null;


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String Url=""jdbc:odbc:driver={SQL D778EAB2E; Database =qlCuocHen; UserName=sa; Password=123456";"; ketNoi= DriverManager.getConnection(Url); ResultSet kq=null; Statement stCmd = this.ketNoi.createStatement();

Server};Server=BODUA-

(Process the results)


M R

executeUpdate DDL: Data Definition Language K


S

S Q

SQL Q

SQL

public ResultSet executeQuery(String strQuery) throws SQLException R .next C C P S

.getString R S T

C .getBoolean(tenCot); .getByte(tenCot); .getLong(tenCot); .getFloat(tenCot); .getDouble

getInt(tenCot);
C C

VD: G NQ NQ T N Q NQ T S C H

StatementstCmd = ketNoi.createStatement(); S Q = S NQ NQ T NQ N Q ;;

ResultSetkq = stCmd.executeQuery(strQuery);
-- L R S

While (kq.next()){ S S H G

getString getBoolean

NQ T

?N

getString
N ;

NQ ;

S } H
-- L

getInt

NQ

While (kq.next()){ S S S } H G H

getString getBoolean getInt

?N S

getString(2);
N ; T ;

Close the database connection)


H C N H C public void close() throws SQLException J M D

Acces
K T M A D D D D T H H

M C P D A T

Data Source (ODBC)

ODBC

T M A

[Tab] System DSN ODBC ; Finish

Add

ODBC Microsoft Access Setup S T J

-T

Data Source Name D)

testODBC

Select

S ODBC Data Source Administrator

OK N D S

S C J hangHoa ln JTable N P P ODBC ODBC J D A N B

F M hangHoa

JT

(H

C
B C

Thng tin H )
D S JT H B

JTable
JD A A C T JD M M Q C ODBC

JT L
import import import import

R T I ODBC JDBC B C
java.sql.*; java.sql.DriverManager; java.sql.ResultSet; java.sql.SQLException;

JDBC T I

/** * * @author Nguyen Mai Huy */

public class accConnect { private Connection ketNoi = null;


/** *P * @throws Exception */ M

public accConnect() throws Exception {


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

StringUrl="jdbc:odbc:testODBC"; this.ketNoi= DriverManager.getConnection(Url,"","");


} /** *P * *@ N -T *@ K * H * C * @throws SQLException */
D T F R T T H C

R S

S O

public ResultSet GetData(String tbName)throws SQLException { ResultSet kq=null; Statement stCmd = (Statement) this.ketNoi.createStatement(); String sqlQuery = "select * from " + tbName; kq = stCmd.executeQuery(sqlQuery);

return kq; } /** *P * @throws Exception */

Database Source Name

public void Close() throws Exception { if (this.ketNoi != null) this.ketNoi.close();


} }

--N

JTable (B T M

JTable)

/** * @author Nguyen Mai Huy * * JT */ -- K O

AbstractTableModel

public class rsTableModel extends AbstractTableModel {


JT

/** *C R *@ -R S * @throws SQLException */

private Vector colHeaders; private Vector tbData;


S

-- C -- P D JT M

F JT

public rsTableModel(ResultSet rsData) throws SQLException { ResultSetMetaData rsMeta = rsData.getMetaData(); -M int count = rsMeta.getColumnCount(); -F colHeaders = new Vector(count); tbData = new Vector();
--- L

D R

R S

for (int i = 1; i <= count; i++) { colHeaders.addElement(rsMeta.getColumnName(i)); } while (rsData.next()) {


--- K -- L O --- L R R S A D

T T

Vector dataRow = new Vector(count);

/** *

for (int i = 1; i <= count; i++) { dataRow.addElement(rsData.getObject(i)); } tbData.addElement(dataRow);

JTable

/** * * @return */

public int getColumnCount() { return colHeaders.size(); }


JT

* @return */

/** * *@ *@ *@ */

public int getRowCount() { return tbData.size(); }


G

/** * *@ *@ */

public Object getValueAt(int row, int column) { Vector rowData = (Vector) (tbData.elementAt(row)); return rowData.elementAt(column); }
JT

-K

@Override public String getColumnName(int column) { return (String) (colHeaders.elementAt(column)); }

JDialog Form

Swing GUI Forms

Sau

ttHangHoa

Finish

JDialog bangDL.

JT

L S JT T JD ttHangHoa D JT bangDL JDialog ttHangHoa P

-P T

T JT M

JT

?C

You might also like