Using JDBC to connect to a database in eclipse

Source: Internet
Author: User
Tags connect to microsoft sql server microsoft sql server microsoft sql server 2005

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.