Welcome to the Java Community Forum and interact with 2 million technical staff to access the className and urlJDBC connections for each database through JDBC. DB2Driver; privateStringurljdbc: db2: localhost: 8080lwc; JDBC connection to MicrosoftSQLServer
Welcome to the Java Community Forum and interact with 2 million technical staff> go to JDBC to connect the className and url JDBC to DB2 private String className = "com.ibm.db2.jdbc.net. DB2Driver "; private String url =" jdbc: db2: // localhost: 8080/lwc "; JDBC connection to Microsoft SQLServer
Welcome to the Java Community Forum and interact with 2 million technical staff> enter
The className and url of JDBC connection to each database
JDBC connection to DB2
Private String className = "com.ibm.db2.jdbc.net. DB2Driver ";
Private String url = "jdbc: db2: // localhost: 8080/lwc ";
JDBC connection to Microsoft SQLServer (microsoft)
Private String className = "com. microsoft. jdbc. sqlserver. SQLServerDriver ";
Private String url = "jdbc: microsoft: sqlserver ://
Localhost: 1433; SelectMethod = Cursor; dataBaseName = lwc ";
JDBC connection Sybase (jconn2.jar)
Private String className = "com. sybase. jdbc2.jdbc. SybDriver ";
Private String url = "jdbc: sybase: Tds: localhost: 2638 ";
JDBC connection MySQL (mm. mysql-3.0.2-bin.jar)
Private String className = "org. gjt. mm. mysql. Driver ";
Private String url = "jdbc: mysql: // localhost: 3306/lwc ";
JDBC connection to PostgreSQL (pgjdbc2.jar)
Private String className = "org. postgresql. Driver ";
Private String url = "jdbc: postgresql: // localhost/lwc ";
Connect JDBC to Oracle (classes12.jar)
Private String className = "oracle. jdbc. driver. OracleDriver ";
Private String url = "jdbc: oracle: thin: @ localhost: 1521: lwc ";
JDBC database connection case
Package com. itlwc;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Public class DBConnection {
Private static Connection conn = null;
Private String user = "";
Private String password = "";
Private String className = "com. microsoft. jdbc. sqlserver. SQLServerDriver ";
Private String url = "jdbc: microsoft: sqlserver ://"
+ "Localhost: 1433; SelectMethod = Cursor; dataBaseName = lwc ";
Private DBConnection (){
Try {
Class. forName (this. className );
Conn = DriverManager. getConnection (url, user, password );
System. out. println ("database connected successfully ");
} Catch (ClassNotFoundException e ){
System. out. println ("failed to connect to the Database ");
} Catch (SQLException e ){
System. out. println ("failed to connect to the Database ");
}
}
Public static Connection getConn (){
If (conn = null ){
Conn = (Connection) new DBConnection ();
}
Return conn;
}
// Close the database
Public static void close (ResultSet rs, Statement state, Connection conn ){
If (rs! = Null ){
Try {
Rs. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Rs = null;
}
If (state! = Null ){
Try {
State. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
State = null;
}
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Conn = null;
}
}
// Test whether the database connection is successful
Public static void main (String [] args ){
GetConn ();
}
}
Basic CURD
For database connection information, check JDBC connection to common databases.
Private Connection conn = DBConnection. getConn ();
Add Method
Increase in the use of spell SQL
Public void add1 (Student student ){
String SQL = "insert into student values (" + student. getId () + ",'"
+ Student. getCode () + "','" + student. getName () + "',"
+ Student. getSex () + "," + student. getAge () + ")";
PreparedStatement ps = null;
Try {
Ps = conn. prepareStatement (SQL );
Ps.exe cuteUpdate ();
} Catch (SQLException e ){
E. printStackTrace ();
}
DBConnection. close (null, ps, conn );
}
[1] [2] [3]