package work;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class database {
public static void fun() throws ClassNotFoundException, SQLException{
System.out.println("****************");
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=TBDB;user=sa;password=446370009";//sa身份连接
//String url2 = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydbdemo;integratedSecurity=true;";//windows集成模式连接
// Declare the JDBC objects.
Connection con = null;
Statement str = null;
ResultSet rs = null;
// Establish the connection.
System.out.println("begin.");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("1111");
con = DriverManager.getConnection(url); //有的传三个参数,格式不一样
System.out.println("end.");
System.out.println(con);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * FROM USERS";
str = con.createStatement();
rs = str.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("U_name"));
System.out.println(rs.getString("U_password"));
}
str.close();
con.close();
}
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
fun();
}
}