当前位置: 技术问答>java相关
连接数据库的 问题?
来源: 互联网 发布时间:2015-04-09
本文导语: 当我使用jsp和sql_server进行连接的时候,我如果不配置odbc去连接数据库..我想只用jdbc去连接,那么这段代码怎么编写呢? | 去微软去下载一个sql server2000 jdbc驱动,然后放置在web-inf/lib中,然后这...
当我使用jsp和sql_server进行连接的时候,我如果不配置odbc去连接数据库..我想只用jdbc去连接,那么这段代码怎么编写呢?
|
去微软去下载一个sql server2000 jdbc驱动,然后放置在web-inf/lib中,然后这样使用:
private static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Mechanism";
private static Connection conn = null;
private static Statement stmt = null;
try
{
Class.forName(sDBDriver);
}
conn = DriverManager.getConnection(sConnStr,"user","111111");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
private static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Mechanism";
private static Connection conn = null;
private static Statement stmt = null;
try
{
Class.forName(sDBDriver);
}
conn = DriverManager.getConnection(sConnStr,"user","111111");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
|
String driverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String connectUrl = "jdbc:microsoft:sqlserver://localhost:1433";
String userName = "sa";
String userPass = "";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(connectUrl,userName,userPass);
conn.setCatalog("dbName");
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
String sqlStr = "select ..... ";
rs = st.executeQuery(sqlStr);
while ( rs.next() ) {
//do something
}
rs.close();
st.close();
String connectUrl = "jdbc:microsoft:sqlserver://localhost:1433";
String userName = "sa";
String userPass = "";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(connectUrl,userName,userPass);
conn.setCatalog("dbName");
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
String sqlStr = "select ..... ";
rs = st.executeQuery(sqlStr);
while ( rs.next() ) {
//do something
}
rs.close();
st.close();
|
用第三方的,比如merant的
到http://industry.java.sun.com/products/jdbc/drivers
去找吧
到http://industry.java.sun.com/products/jdbc/drivers
去找吧