黑马程序员技术交流社区

标题: JDBC连接SQL [打印本页]

作者: MrDevil    时间: 2016-6-3 14:18
标题: JDBC连接SQL
import java.sql.*;


public class test012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://localhost:64690; DatabaseName=test";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Statement st;
Connection con;
try {
Class.forName(driver);// 加载驱动
System.out.println("驱动加载成功!");
} catch (ClassNotFoundException event) {
System.out.print("无法创建驱动程式实体!");
}
try {
con = DriverManager.getConnection(url, "sa", "sa123456");
con.setAutoCommit(true);
System.out.println("已经连接到数据库...");
st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * from a ");
System.out.println("id"+"      "+"date");
while (rs.next()) {
String id = rs.getString("id");
String date = rs.getString("date");

System.out.println(id+""+date);
}

st.close();
con.close();
} catch (SQLException e1) {
System.out.println("异常" + e1);
}
}
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2