Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url =
"jdbc:microsoft:sqlserver://super-happy:1433;DatabaseName=test";
String user = "sa";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement();
String sql = "select * from test";
ResultSet rs = statement.executeQuery(sql);
/*格式如下(与上例无关,双引号可改成单引号)
statement.execute("insert into test values("+"\""+IP+"\""+","+"\""+Id+"\""+","+"\""+Sn+"\""+","+"\""+time+"\""+")"); statement.execute("update test set IP="+"\""+ip+"\" +",Id="+"\""+Id+"\" +",Sn="+"\""+USn +"\""+",UTime="+"\"" +UTime+"\""+" where User_Id="+"\""+tem_id+"\"");
String url =
"jdbc:microsoft:sqlserver://super-happy:1433;DatabaseName=test";
String user = "sa";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement();
String sql = "select * from test";
ResultSet rs = statement.executeQuery(sql);
/*格式如下(与上例无关,双引号可改成单引号)
statement.execute("insert into test values("+"\""+IP+"\""+","+"\""+Id+"\""+","+"\""+Sn+"\""+","+"\""+time+"\""+")"); statement.execute("update test set IP="+"\""+ip+"\" +",Id="+"\""+Id+"\" +",Sn="+"\""+USn +"\""+",UTime="+"\"" +UTime+"\""+" where User_Id="+"\""+tem_id+"\"");
/*实现封装数据库操作的JavaBean基类该类提供数据库的操作方法*/
package db;import java.net.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;public class DBConnect
{
static Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//构造函数
public DBConnect(){}
public static String getConnection()
{
try{
//注册数据库驱动为SQL2000驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//建立数据库连接,使用SQL2000的一种连接方式,localhost指主机名字,eZsoft为数据库,后面的sa和 //为用户名和密码
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver:localhost:1433;DatabaseName=eZsoft","sa","");
return "数据库连接成功!";
}
catch(java.lang.ClassNotFoundException e){
return "驱动加载失败靠"+"ClassNotFoundException:"+e.getMessage();
}
catch(java.sql.SQLException e){
return "连接数据库错误了么"+"SQLExcception:"+e.getMessage();
}
} //关闭数据库连接
public void closeConnection()
{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(java.sql.SQLException e)
{
System.err.println("SQLBridge():"+e.getMessage());
}
rs=null;
stmt=null;
conn=null;
}
//exeuteQuery方法用于记录查询
//入口参数为sql语句,返回ResultSet对象
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
if(conn==null)
getConnection();
if(conn!=null)
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//执行数据库查询操作
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("SQLBridge.executeQuery:"+ex.getMessage());
}
return rs;
} //executeUpdate方法用于进行add或者update记录的操作
//入口参数为sql语句,成功返回true,否则为false
public boolean executeUpdate(String sql)
{
boolean bupdate=false;
try
{
//建立数据库连接
if(conn==null)
getConnection();
if(conn!=null)
{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int rowCount=stmt.executeUpdate(sql);
//如果不成功,bupdate就会返回false
if(rowCount!=0)
bupdate=true;
}
}
catch(SQLException ex)
{
System.err.println("SQLBridge.executeUpdate:"+ex.getMessage());
}
return bupdate;
} //toChinese方法用于将一个字符串进行中文处理
public static String toChinese(String strvalue)
{
try{
if(strvalue==null){
return null;
}
else{
strvalue=new String(strvalue.getBytes("ISO8859_1"),"GBK");
return strvalue;
}
}
catch(Exception e){
return null;
}
}
}