Java database connection and java database connection code
Note:
1. All Database-related (jdbc) packages are java. SQL .*;
2. Copy the jar packages required by the project to the web-inf/lib folder.
I. sqlsever Database
Package dbcon;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Public class DBConn {
Public Connection getCon (){
// Declare the Connection object Connection con = null;
Try {
// Load the driver
Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver ");
// Obtain the database connection
Con = DriverManager. getConnection ("jdbc: sqlserver: // localhost: 1433; databaseName = db_errorsystem", "sa", "sa ");
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
// Return Value
Return con;
}
/**
* @ Param args
*/
// Test whether the connection is successful
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. println (new DBConn (). getCon ());
}
}
Ii. MySQL database
Package dbcon;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Public class DBConnection {
Public Connection getConnection (){
Connection con = null;
Try {
Class. forName ("com. mysql. jdbc. Driver ");
Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/login", "root", "123456 ");
} Catch (Exception e ){
E. printStackTrace ();
}
Return con;
}
}
Iii. Oracle Database
Package cn.com. systop. l03.dbconnection;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. SQLException;
/**
* Database connection
* @ Author Administrator
*
*/
Public class DBConnection {
// Database connection string
Private final static String URL = "jdbc: oracle: thin: @ localhost: 1521: xe ";
// Database logon USERNAME private final static String USERNAME = "scott ";
// Logon Password
Private final static String PASSWORD = "tiger ";
/**
* Obtain the database connection
* @ Return database connection object
*/
Public static Connection getConnection (){
Connection con = null;
Try {
// Load the driver
Class. forName ("oracle. jdbc. driver. OracleDriver ");
// Obtain the database connection
Con = DriverManager. getConnection (URL, USERNAME, PASSWORD );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return con;
}
}
Driver jar packages of each database. To connect to SQL 2008, use sqljdbc4.jar;
Jar package: http://pan.baidu.com/s/1c0hED0k
JDBC in JAVA also has database connection code Problems
RS. GETSTRING (3)
RS is the set that stores the returned data from the database.
The content format is: 1, 2, 3, 4
1 name pwd other
2 wayk 123 ~~~~
3 name 321...
Just like the tables in your database, the above 1234 represents your number of columns.
When you take values from rs, you need to tell rs what type of column you are taking.
Rs. GETSTRING (3) is the 2nd column of the String type (because the rs index starts from 0)
While (rs. next ()){
// Rs. next rs points to 0th rows by default. The rs. next method is used to point to the next row.
I ++;
System. out. println ("th" + I + "Row record ");
System. out. println ("userName:" + rs. getString (3 ));
// Because your userName is the second column in your database and the index of the String type rs starts from 0, 3 is passed here.
// Rs. getString (3)
System. out. println ("userPwd:" + rs. getString (3 ));
}
By the way, why can't I find the fourth column in your database because the length cannot exceed 5? Of course, an error is returned.
JDBC in JAVA also has database connection code Problems
RS. GETSTRING (3)
RS is the set that stores the returned data from the database.
The content format is: 1, 2, 3, 4
1 name pwd other
2 wayk 123 ~~~~
3 name 321...
Just like the tables in your database, the above 1234 represents your number of columns.
When you take values from rs, you need to tell rs what type of column you are taking.
Rs. GETSTRING (3) is the 2nd column of the String type (because the rs index starts from 0)
While (rs. next ()){
// Rs. next rs points to 0th rows by default. The rs. next method is used to point to the next row.
I ++;
System. out. println ("th" + I + "Row record ");
System. out. println ("userName:" + rs. getString (3 ));
// Because your userName is the second column in your database and the index of the String type rs starts from 0, 3 is passed here.
// Rs. getString (3)
System. out. println ("userPwd:" + rs. getString (3 ));
}
By the way, why can't I find the fourth column in your database because the length cannot exceed 5? Of course, an error is returned.