Connect SQL Sever2008 in Eclipse-----go from Yogurshine

Source: Internet
Author: User

Connect SQL Sever2008 in eclipse

-----Transferred from Yogurshine

A SQl Sever server configuration

1 I have installed SQL Sever 2008R2 before. (Note: Installation is not successful, be sure to uninstall clean, otherwise not full, toss for a long time)

2. When connecting to the server, there are generally multiple choices: (This server name is the database instance set at installation, the default instance has the user name mit-pc and mti-pc\sqlexpress, and some of them are installed with only the user name, this instance does not affect the selection of the user name instance.) )

3. The general JSP operation when multiple select SQLExpress server, but it is disabled by default and the port number is not configured, so the JDBC connection to SQL Server to be reset, configured as follows:

Open SQL Sever Configuration Manager, SQLExpress in the Protocol tcp/ The IP settings ipall TCP dynamic port is 1433. (Some of the following TCP port default is 1433, note that the system can not have port conflicts, have not tried to set the other line no, I succeeded at once) restart takes effect. Also note that the first three protocols on the right column are open and via is disabled.

4. Advanced in with Windows authentication. (This is in any case the default can go in, if not into the installation is not good.) Re-installed. After you go in, set up Add users, set user permissions, and set up to log in as the following SQL Sever authentication, which means that users who have added permissions for any database are under Windows Authentication entry, if the SQL Sever Authentication Logon Settings The error is generally reported when modifying permissions.

Continue text:

After entering, create a new database sample, add user test, set

After setting up, re-enable SQL Sever authentication, try the user set above, log on successfully. This sqlexpress server configuration is complete.

5. Another new database instance I installed myself Sqllei can also be used, the basic settings are the same as above. The port here is the default 50573

Login successfully with the user jsptest I set up

New databases and tables are created for easy testing in Java below.

New database jspdb, new table AAAA, added user jsptest

Note: 1. The user and the database are mapped to indicate that this user can access the operation of the database.

2. Database name, table name can not use the reserved name, such as the previous table name to user, the result has been an error.

Two JDBC-connected driver installations for SQL Server

1. Download: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774

Download "Sqljdbc_4.0.2206.100_enu.exe" to get Sqljdbc_4.0.2206.100_enu.exe, extract to any directory. and find the Sqljdbc4.jar file in the directory, get its path start configuration environment variable

Append C:\lei\Android\sqljdbc_4.0\enu\sqljdbc4.jar to the environment variable classpath (note that there are two jar packages in the directory, and the general SQL Sever 2000 is Sqljdbc4.jar.

Three import packages in Java, test.

1 window->preference-->java->installed jre-->edit-->add External Jars, find directory Add Sqljdbc4.jar

2 new Java Project,

Package Lei.com.cn;import java.sql.*;//Before you use JDBC, you import a class about SQL before the file. public class Test {public static void main (string[] args) {//TODO auto-generated Method Stub//Get Database connected connect string Drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//Load JDBC Driver "String dburl=" Jdbc:sqlserver ://localhost:50573; Databasename=jspdb ";   JDBC protocol String userName = "Jsptest";   User name (ensure that the user and database have a mapping relationship, that is, access) String userpwd = "*********";              Password Connection dbconn=null;                  try {class.forname (drivername); Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);   Connect server and Database jspdb System.out.println ("Connection successful!");                  If the connection succeeds console output connection successful!                     } catch (Exception e) {e.printstacktrace (); }//Build Statement Object Statement St=null;//statement provides the ability to perform basic SQL statement operations try {st = Dbconn.Createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);        } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();        }//Execute SQL statement String sql= "select * from AAAA";        ResultSet rs=null;//Save the returned result set try {rs = st.executequery (sql);//execute the SQL statement with the ExecuteQuery () method of the statement instance        } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }//Get query result try {while (Rs.next ()) {System.out.println ("first field content is:" +rs.getstring (1))                ;                System.out.println ("The second field content is:" +rs.getstring (2));                            System.out.println ("The third field content is:" +rs.getstring (3));        }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } System.out.println ("Operation succeeded!        "); Close created object (open first close) try {Rs.cloSE ();            St.close ();        Dbconn.close ();                } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }    }    }

Test results:

Connection successful!
The first field content is: 1
The second field contains: Admin
The third field contains: Admin
The first field content is: 2
The second field contains the following: Lei
The third field content is: 123
The first field content is: 3
The second field content is: hehe
The third field content is: 456
Operation Successful!

PS: If you have used JDBC to connect to SQL Server 2000, you should note:

The statements that load the driver and URL paths in SQL Server 2000 are

String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";

String dburl = "jdbc:microsoft:sqlserver://localhost:1433; Databasename=sample";

The statements that load drivers and URLs in SQL Server 2005 are

String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";

String Dburl = "jdbc:sqlserver://localhost:1433; Databasename=sample ";

Connect SQL Sever2008 in Eclipse-----go from Yogurshine

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.