Today, I wrote a class to connect to SQL Server through JDBC for beginners to refer to. Recently, I started to read Java because I had to do a job. net, it seems that there are many similarities between the two platforms.
Test code of the database. Due to limited time, no comments are written. in SQL Server 2000, the Code passes the test:
Create Database test on
(
Name = 'test _ data ',
Filename = 'd:/project/Java/dbtest2/test_data.mdf ',
Size = 1 MB,
Maxsize = 10 MB
)
Log On
(
Name = 'test _ log ',
Filename = 'd:/project/Java/dbtest2/test_log.ldf ',
Size = 1 MB,
Maxsize = 5 MB
)
Use test
Create Table friends
(
F_id int identity (1, 1) primary key,
F_name varchar (20) not null,
F_address varchar (30 ),
F_tel varchar (19) unique,
F_hiredate datetime,
F_salary money
)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('shawn ', 'chengdu', 150008712341, '2017-11-12', 2006)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('chris ', 'northoa', 150008712321, '2017-5-22', 2005)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('calla', 'hangzhou', 150002512341, '2017-7-6 ', 2004)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('Sean ', 'chengdu', 150002716341, '2017-1-1', 2006)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('Matt ', 'mahiam', 150008718541, '2017-4-25', 2006)
Insert into friends (f_name, f_address, f_tel, f_hiredate, f_salary) values ('john', 'usa', 150008582341, '2017-2-28 ', 2003)
Select * from friends
The following is the Java code. Complete the ODBC configuration of the database by yourself:
/*
Test code for connecting to SQL Server using Java. It is only for beginners.
Shawn @ Copyleft
*/
Import java. SQL. *; // the package to be added
// Class definition
Class dbconnect {
Private string con1 = "Sun. JDBC. ODBC. jdbcodbcdriver"; // string required to connect to SQL
Private string url = "JDBC: ODBC: Test ";
Private string user = "sa", password = ""; // modify the password based on your database user and password.
Connection con; // used to connect to the database
Preparedstatement pS; // you can also use statement. preparedstatement integrates statement.
Resultset RS; // a set that can be used to execute SQL commands
// Constructor
Dbconnect (){
Try {
Class. forname (con1); // class. forname () is used to load some classes to JVM.
This. Connect (); // function call
Try {
This.exe cute ();
} Catch (sqlexception ex ){
System. Out. println (ex. tostring ());
}
} Catch (classnotfoundexception CE ){
System. Out. println (CE );
}
}
Public void connect (){
Try {
Con = drivermanager. getconnection (URL, user, password); // configure ODBC before doing this
If (con! = NULL ){
System. Out. println ("connection sucessfully! ");
}
} Catch (sqlexception ex ){
System. Out. println (ex. tostring ());
}
}
Public void execute () throws sqlexception {
PS = con. preparestatement ("select * from friends"); // run the SQL statement to PS
Rs = ps.exe cutequery (); // run the command here, and then let the RS know the information.
While (Rs. Next () // next () must be added here, And the offset is moved.
{
System. Out. Print (Rs. getstring (2) + "/t ");
System. Out. Print (Rs. getstring (3) + "/t ");
System. Out. Print (Rs. getstring (4) + "/t ");
System. Out. Print (Rs. getdate (5) + "/t ");
System. Out. Print (Rs. getint (6) + "/t ");
System. Out. println ("");
}
}
Public void close () // used to release resources. Java does not have destructor, but protected void finalize () is rewritten (),
{// Call system. runfinalization () and system. GC () to remind JVM to execute finalize () for release,
Try {// In the previous j2se version, you can call finalize () using the above method. However, currently j2se5.0 can only remind JVM, but not necessarily execute JVM.
Rs. Close (); // the best solution is to write the structure close () by yourself ();
PS. Close ();
Con. Close ();
} Catch (sqlexception CE)
{
System. Out. println (Ce. tostring ());
}
System. Out. println ("connection released !!! ");
}
Public static void main (string [] ARGs ){
Dbconnect DBC = new dbconnect ();
DBC. Close ();
}
}