One: Take the example of using JDBC to connect a Microsoft SQL Server 2000 database in eclipse:
1. Open Microsoft SQL Server 2000 Service Manager
2. Create a new database in Microsoft SQL Server 2000 Student
3. Open Eclipse, new Project Project test, new package test, new class Conn
4. Write the code as follows
1 Packagetest;2 3 ImportJava.sql.*;4 5 Public classConn {//Create Class Conn6Connection con;//declaring connection Objects7 8 PublicConnection getconnection () {//to establish a method that returns a value of connection9 Try{//Load the database driver classTenClass.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"); OneSYSTEM.OUT.PRINTLN ("Database driver loading succeeded"); A}Catch(ClassNotFoundException e) { - e.printstacktrace (); - } the Try{//get a database connection object by accessing the URL of the database -Con =DriverManager -. getconnection ("Jdbc:microsoft:sqlserver" -+ "://localhost:1433;databasename=student", "sa", +"Jiqiang"); -SYSTEM.OUT.PRINTLN ("Database connection succeeded"); +}Catch(SQLException e) { A e.printstacktrace (); at } - returnCon//returns a Connection object as required by method - } - - Public Static voidMain (string[] args) {//Main Method -Conn C =NewConn ();//create this class object inC.getconnection ();//Call the Connect database method - } to}
5. Running the debugger, there is an error
6. Here Baidu Google, found the cause of the error: missing jar package, because the connection to Microsoft SQL Server 2000 version requires the configuration of three jar packages,
7. After downloading this jar package from the Internet, you need to configure the build path, right-click Project Project, select Build Path--Configure the build path
8. Select the Library--add an external jar, select three jar packages, click OK
9. At this point, you can find that there is a reference library in the Project project, which indicates that the configuration was successful
10. Run again, the connection is successful
II: Summary (partly from http://www.cnblogs.com/xiohao/p/3507483.html)
1. Connect to Microsoft SQL Server 2000 (Note Configuration jar package: Msbase.jar,mssqlserver.jar,msutil.jar)
Class.forName ("Com.microsoft.JDBC.sqlserver.SQLServerDriver");
String url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=testdb";
String user= "Test"; String password= "Test";
Connection con=drivermanager.getconnection (Url,user,password);
2. Connect to Microsoft SQL Server 2005 (Note Configuration jar package: Sqljdbc.jar)
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url= "Jdbc:sqlserver://localhost:1433;databasename=testdb";
String user= "Test"; String password= "Test";
Connection con=drivermanager.getconnection (Url,user,password);
3. Connect Oracle 8/8i/9i/10g/11g (thin mode)
Class.forName ("Oracle. JDBC.driver.OracleDriver "). newinstance ();
String url= "JDBC:oracle:thin: @localhost: 1521:orcl"//ORCL the SID for the Oracle database
String user= "Test";
String password= "Test";
Connection con=drivermanager.getconnection (Url,user,password);
4. Connect to MySQL database (Note Configuration jar package: Mysql-connector-java-5.1.6-bin.jar)
Class.forName ("Com.mysql.jdbc.Driver");
String url= "Jdbc:mysql://localhost:8080/testdb";
String user= "Test"; String password= "Test";
Connection con=drivermanager.getconnection (Url,user,password);
5. Connect to an Access database
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D bq=" +application.getrealpath ("/data/testdb/mdb");
Connection conn=drivermanager.getconnection (URL, "", "");
Using JDBC to connect to a database in eclipse