Package xhs;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.Statement;
public class Mystringclass {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated method stubs
String Connectionurl = "jdbc:sqlserver://localhost:1433;"
+ "databasename=adventureworks;integratedsecurity=true;";
String url = "jdbc:sqlserver://127.0.0.1:1433;databasename=qqhrlgzysyweb;user=sa;password=123";//sa identity Connection
String url2 = "jdbc:sqlserver://127.0.0.1:1368;databasename=mydbdemo;integratedsecurity=true;"; /windows Integrated Mode connection
Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Establish the connection.
SYSTEM.OUT.PRINTLN ("begin.");
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = drivermanager.getconnection (URL);
System.out.println ("end.");
Create and execute an SQL statement that returns some data.
String SQL = "Select TOP Ten * from Newsinfo";
stmt = Con.createstatement ();
rs = Stmt.executequery (SQL);
Iterate through the data in the result set and display it.
while (Rs.next ()) {
System.out.println (Rs.getstring (4) + "" + rs.getstring (6));
}
}
Handle Any errors there is a occurred.
catch (Exception e) {
E.printstacktrace ();
}
finally {
if (rs! = null)
try {
Rs.close ();
} catch (Exception e) {
}
if (stmt! = null)
try {
Stmt.close ();
} catch (Exception e) {
}
if (con! = null)
try {
Con.close ();
} catch (Exception e) {
}
}
}
}
SA authentication Mode
SQLSERVER2008 is a dynamic port that you need to configure:
Open the Configuration Tool->sqlserver Configuration Manager->sqlserver Network configuration->mssqlserver Protocol->TCP/IP enabled, remove 0 from the TCP dynamic port, leave it blank, and then pull the list to the bottom (IPALL ), configure a fixed port, you can use this port to connect to the database at a later time: