JDBC Connection to SQL Server

Source: Internet
Author: User
Tags microsoft sql server stmt

Download the JDBC driver package

, I downloaded the EXE version, in fact, is a self-extracting package. After the download is complete, the double-click Run will be unzipped in the current directory.

The Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\chs\auth\x64\sqljdbc_auth.dll file is copied to the C:\Windows\SysWOW64 directory. (64bit system)

Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\chs\auth\x86\sqljdbc_auth.dll file is copied to C:\Windows\System32 (32-bit system, not validated)

The Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\chs\sqljdbc4/jar package is then imported into the project.

Source

 1 Import java.sql.*; 2 public class conn{3 public static void main (string[] args) {4 String jdriver= "Com.microsoft.sqlserver.jdbc.s Qlserverdriver "; 5//sql Database Engine 6 String connectdb= "Jdbc:sqlserver://abyss-pc\\myabyss:64160;databasename=master"; 7//Data Source!!!        Note the failure to load or connect to the database is usually a problem here 8//I will detail the following 9 try {10//Load Database engine, return the given string name of Class one class.forname (Jdriver); 12 }catch (ClassNotFoundException e), {//e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Load Database engine lost           System.exit (0), and System.out.println ("Database-driven success"); try {21                                    String user= "SA"; 22//Here just pay attention to user name password do not write wrong can be password= String "*liluotinghua33*"; Connection con=drivermanager.getconn Ection (Connectdb,user,password); 25//Connect Database object System.out.println ("Connection database succeeded"); Statement Stmt=con.cre     Atestatement (); 28//Create SQL Command object 29 30//CREATE TABLE 31       System.out.println ("Start create TABLE"); 32//CREATE TABLE SQL statement query= String "CREATE TABLE TABLE1 (ID 34 NCHAR (2), NAME NCHAR (Ten)) "; stmt.executeupdate (query);//Execute SQL Command object Syste M.OUT.PRINTLN ("Table creation succeeded"); 37 38//Input data System.out.println ("Start inserting data"); Strin G a1= "INSERT into TABLE1 VALUES (' 1 ', ' Asahi Brothers ')"; 41//INSERT Data SQL statement String a2= "insert into TABLE1 VALUE S (' 2 ', ' Viagra '), "a3=" INSERT into TABLE1 VALUES (' 3 ', ' brother Zhang ') ", Stmt.executeupdate (A1);//Execute SQL command on   Like Stmt.executeupdate (A2);            Stmt.executeupdate (A3), System.out.println ("Insert data Success"), 48 49//Read Data 50 System.out.println ("Start reading Data"), Wuyi ResultSet rs=stmt.executequery ("SELECT * from TABLE1" );//Return SQL statement query result set (collection) 53//loop output each record is (Rs.next ()) {55//output per field System.out. println (Rs.getstring ("ID") + "\ T" +rs.getstring ("NAME");}58 System.out.println ("read Complete"); 59 60 Close connection stmt.close ();//Close Command object connection Con.close ();//Close database connection}catch (SQLException e) {e.printst Acktrace (); System.out.print (E.geterrorcode ());//system.out.println ("Database connection Error"); System.exit (0)     ; 68}69}70}

Problems that arise

String connectdb= "Jdbc:sqlserver://abyss-pc\\myabyss:64160;databasename=master";

Let's take a look at this piece of code.

Where Abyss-pc\\myabyss represents the server name, and two backslashes indicate escape. What is the name of the server, such as:

64160 is the port number, in general the default is 1433, in special cases, as mine is randomly assigned.

Verify port number: 1, open Task Manager, select Service, view the PID number of the SQL Server instance is 7284, my computer runs two DB instances, I only connect the first instance. 2. Open the command prompt. Enter Netstat-ano. 7284 the corresponding port number is 64160.  Databasename=master; Master represents the data source: One problem with running is that you can log on to the database with Windows authentication, but the use of SQL Server Authentication prompts you to fail. In general, error codes are reported, and the error code I report here is 18456. You can search for it with Google: SQL Server 2008 error code 18456. Here I simply provide a solution. First, Windows accounts can be logged in.
The process of finding the problem:
The first step: start all SQL-related services, the problem remains;
The second step: View Windows Firewall, is started by default, does not start the selection is disabled. Two tips "For security reasons, some settings are controlled by Group Policy" "Windows Firewall is using your domain settings", First use Gpedit.msc to hit the Group Policy Editor, machine configuration--Management template--network--Network connection--windows Firewall--standard configuration file, to see the right options properties, are not configured, the problem is not here.
Third step: Log in with Windows account, after successful connection, right click on the instance, select Properties---security. Determine that server authentication selects SQL server and Windows Authentication mode the problem is almost resolved with these three steps. Also, don't forget to log in to the SA user. Double-click the sa account selection status.

JDBC Connection to SQL Server

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.