Eclipse Java Neon connects to SQL Server2008 via JDBC

Source: Internet
Author: User

JDBC (Java Database Connectivity,java connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language.

1. Configure SQL Server: Start > Microsoft SQL Server2008 > SQL Server Configuration Manager > SQL Server Network Configuration > SQLEXPRESS protocol > TCP/IP

TCP/IP configuration: Modify IP is an entry for "127.0.0.1" and Ipall, and after the modification is complete, make sure that TCP/IP is turned on

2. Create a database Myhotel in SQL Server2 and set up related tables

The code in 3.Eclipse is as follows:

1Import java.sql.*;23ClassGetcon {4Connection con;5//Get a good connection to the database Myhotel connection6PublicConnection Getcon () {7Try{8 Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");9}Catch(ClassNotFoundException e) {10System.out.println (e);11}12Try{String url = "Jdbc:sqlserver://127.0.0.1:1433;datebasename= Myhotel";String saname = "sa";String SAPW = "123";+ con =Drivermanager.getconnection (URL,SANAME,SAPW);17}Catch(SQLException e) {18System.out.println (e);19}20ReturnCon21st}22}2324PublicClassTest1 {25PublicStaticvoidMain (string[] args) {Getcon GC =NewGetcon ();27Connection con;28Statement SQL;29ResultSet rs;30Try{+ con =Gc.getcon ();sql =Con.createstatement ();33//Query database Myhotel The price of a single room in a hotel table in the Dubai Burj Al Arabrs = sql.executequery ("Select Price from hotel"+ "where hname = ' Dubai sailing hotel ' and Rmtype = ' single room ');  if (Rs.next ()) {PNs System.out.println (rs.getstring ("Price" ));   (SQLException e) { e.printstacktrace ();                 (a) 

#错误总结 #

1. sqljdbc4.0 Error not introduced:

Java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver
Java.sql.SQLException:No suitable driver found for jdbc:sqlserver://127.0.0.1:1433;datebasename= Myhotel
Exception in thread "main" java.lang.NullPointerException
At Test1.main (test1.java:32)

Workaround: Testhotel (created Java project) > Properties > Java Build Path > Add External JARs ... > select download Good sqljdbc4.jar > OK

2. Default database is not set

Com.microsoft.sqlserver.jdbc.SQLServerException: Object name ' Hotel ' is not valid.
At Com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError (sqlserverexception.java:216)
At Com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult (sqlserverstatement.java:1515)
At Com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement (sqlserverstatement.java:792)
At Com.microsoft.sqlserver.jdbc.sqlserverstatement$stmtexeccmd.doexecute (sqlserverstatement.java:689)
At Com.microsoft.sqlserver.jdbc.TDSCommand.execute (iobuffer.java:5696)
At Com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand (sqlserverconnection.java:1715)
At Com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand (sqlserverstatement.java:180)
At Com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement (sqlserverstatement.java:155)
At Com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery (sqlserverstatement.java:616)
At Test1.main (test1.java:34)

Solution 1: Add Myhotel before the table used. (Note is two points!!) ) is "Database +.. + Table Name "

Workaround 2: Modify the default database, select the login used when connecting in SQL Server (my is SA) sa > Properties > General > Default Database > select the desired database (my is Myhotel)

Eclipse Java Neon connects to SQL Server2008 via JDBC

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.