当前位置: 技术问答>java相关
新手提问,JSP中连接SQLSERVER数据库,加再驱动程序和建立连接的代码如何写?
来源: 互联网 发布时间:2017-04-16
本文导语: 新手提问,JSP中连接SQLSERVER数据库,加再驱动程序和建立连接的代码如何写? | package note; import java.sql.*; import java.util.*; public class cn { String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String ...
新手提问,JSP中连接SQLSERVER数据库,加再驱动程序和建立连接的代码如何写?
|
package note;
import java.sql.*;
import java.util.*;
public class cn
{
String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://172.18.24.222:1433;DatabaseName=wap";
String user="sa";
String pass="";
Connection conn=null;
ResultSet rs=null;
public String[] split(String s,String c) {
if(s.indexOf(c)==-1) return new String[]{s};
StringTokenizer st=new StringTokenizer(s,c);
String[] str=new String[st.countTokens()];
for(int i=0;st.hasMoreElements();i++) {
str[i]=st.nextToken();
}
return str;
}
public cn() {
try {
Class.forName(cline);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("classnotfound:"+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
}
}
需要下载相应的jdbc
import java.sql.*;
import java.util.*;
public class cn
{
String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://172.18.24.222:1433;DatabaseName=wap";
String user="sa";
String pass="";
Connection conn=null;
ResultSet rs=null;
public String[] split(String s,String c) {
if(s.indexOf(c)==-1) return new String[]{s};
StringTokenizer st=new StringTokenizer(s,c);
String[] str=new String[st.countTokens()];
for(int i=0;st.hasMoreElements();i++) {
str[i]=st.nextToken();
}
return str;
}
public cn() {
try {
Class.forName(cline);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("classnotfound:"+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
}
}
需要下载相应的jdbc