package mydbproject;
import java.sql.*;
public class MyDB {
public static void main(String[] args) {
//加载驱动程序
try {
//桥连接
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//直接连接
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
}
//连接字符串
//桥连接
//String str = "jdbc:odbc:test";
//直接连接
String str = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=studb";
try {
Connection con = DriverManager.getConnection(str,"sa","sa");
Statement sta = con.createStatement();
ResultSet res = sta.executeQuery("select * from student");
while(res.next()) {
System.out.print(res.getInt(1));
System.out.print(res.getString("s_name"));
System.out.print(res.getString(3));
System.out.println();
}
//sta.executeUpdate("insert into student(s_name,s_sex,s_add,s_email)" +
// "values('aaa',1,'aaa','aaa@126.com')");
res.close();
sta.close();
con.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
}
java连接sqlserver数据库示例以及jar包
4星 · 超过85%的资源 需积分: 33 125 浏览量
2007-11-13
22:43:45
上传
评论 9
收藏 504KB RAR 举报
mxli0201
- 粉丝: 3
- 资源: 21
- 1
- 2
- 3
- 4
前往页