82870034 发表于 2016-10-30 00:19:29

myeclipse连接SQL server 2012

package jdbc;
import java.io.*;
import java.util.*;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class jdbc {
public static void main(String[] args) {
jdbc();}
static void jdbc()
{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");                           
System.out.println("加载驱动成功");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=GreenHome";
Connection conn=DriverManager.getConnection(url, "sa", "123");
System.out.println("连接成功:");
Statement stmt=conn.createStatement();
String sql=sql();                  
stmt.execute(sql);
stmt.close();
conn.close();
}                //异常处理
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//输入要执行的sql语句
static String sql()
{
Scanner input= new Scanner(System.in);
String sql=input.nextLine();
return sql;
}
}
  
页: [1]
查看完整版本: myeclipse连接SQL server 2012