The following is a database connection method for several common database systems.

xiaoxiao2021-03-05  23

The following is a database connection method for several common database systems.

System environment: Win2003 J2SE5.0 Tomcat5.5.7

The premise is that you have already downloaded JDBC support from all databases from the Internet! Put those major toolkits in Tomcat 5.5 / Common / LIB! Planing Things that get started, now start to enter the topic! In order to reduce the space, the problem is more intuitive, I have put an exception capture code to remove the code!

1.Microsoft SQL Server 2000

Class.Forname ("com.microsoft.jdbc.sqlser.sqlserdriver); connection conn = drivermanager.getConnection (" JDBC: Microsoft: SQL Server: // localhost: 1433; DatabaseName = Database Name "," SA "," PWD " ); Statement stmt = conn.createStatement (); // increase and query statement stmt.executeUpdate ("INSERT INTO BOYA VALUES"); ResultSet RS = Statement.executeQuery ("Select * from boya "); // Display record while (rs.next ()) {out.print (rs.getstring (1) " rs.getstring (2)); out.println ("
");}

2.MYSQL 5.0

Class.Forname ("com.mysql.jdbc.driver); connection conn = drivermanager.getConnection (" jdbc: mysql: // localhost / database name? User = root & password = pwd "); statement stmt = conn.createstatement () ; // Increase and query statement Stmt.executeUpdate ("ISERT INTO BOYA VALUES"); ResultSet RS = Stmt.executeQuery ("SELECT * from boya"); // Displaying the record while (RS .next ()) {Out.print (rs.getstring (1) " rs.getstring (2)); out.println ("
");}

3.PostgreSQL 8.0

// The default port 5432 of this server, if the code needs to change Class.Forname ("org.postgreSql.driver); connection conn = drivermanager.getConnection (" JDBC: PostgreSQL: // localhost: 5432 / database name User = admin&& password = pwd "); statement stmt = conn.createstatement (); // increase and query statement stmt.executeUpdate (" INSERT INTO BOYA VALUES "); ResultSet RS = STMT. ExecuteQuery ("Select * from boya"); // Displays the record while (rs.next ()) {out.print (rs.getstring (1) " rs.getstring (2)); Out.println ("
");} 4.odbc

// in my opinion will write ODBC enough, now PostgreSQL, mysql has ODBC support class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver); connection conn = drivermanager.getConnection (" JDBC: ODBC: Test ",", "" "); Statement stmt = conn.createstatement (); // increase and query statement stmt.executeUpdate (" INSERT INTO BOYA VALUES ("MySQL ',' 5.0 ')"); ResultSet RS = STMT. ExecuteQuery ("Select * from boya"); // Displays the record while (rs.next ()) {out.print (rs.getstring (1) " rs.getstring (2)); Out.println ("
");

转载请注明原文地址:https://www.9cbs.com/read-38990.html

New Post(0)