MyOdbc.java的原码
package my.data;
import java.io.IOException;
import java.sql.*;
import java.util.Date;
public class MyOdbc{
private String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//private String driver="oracle.jdbc.driver.OracleDriver";
//private String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//private String driver="com.mysql.jdbc.Driver";
private String url="jdbc:odbc:njhyj";
//private String url="jdbc:oracle:thin:@localhost:1521:sid";
//private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=njhyj";
//private String url="jdbc:mysql://localhost:3360/njhyj";
//private String UserName="sa";
//private String Password="";
private Connection conn;
private Statement stmt;
private ResultSet rs;
private int i;
private boolean boo;
public MyOdbc(){
conn=null;
stmt=null;
rs=null;
}
public Connection getConn(){
try{
Class.forName(driver);
System.out.println ("找到驱动");
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
}catch(Exception e){
System.out.println (e.getMessage());
System.out.println ("找不到驱动");
}
try{
conn=DriverManager.getConnection(url,"sa","");
System.out.println ("连接成功");
}catch(SQLException e){
System.out.println (e.getMessage());
System.out.println ("连接数据错误");
}
return conn;
}
//判断数据库是否有要注册的用户
public boolean regUpdate(String UserName){
this.getConn();
String sql="select UserName from user_table where UserName='"+UserName+"'";
try{
System.out.println (sql);
rs=this.executeQuery(sql);
if(rs.next()){
boo=false;
}else{
boo=true;
}
}catch(SQLException se){
System.out.println (se.getMessage());
System.out.println ("操作表出错MyOdbc 58");
}
return boo;
}
public int executeUpdate(String sql){
try{
System.out.println ("SQL:"+sql);
conn=this.getConn();
stmt=conn.createStatement();
i=stmt.executeUpdate(sql);
}catch(SQLException e){
System.out.println (e.getMessage());
System.out.println ("数据插入出错");
}finally{
this.closeConn();
}
return i;
}
public ResultSet executeQuery(String sql){
try{
System.out.println ("SQL:"+sql);
conn=this.getConn();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println (e.getMessage());
System.out.println ("数据查询出错");
}
return rs;
}
//登录专用
public boolean login(String UserName,String Password){
String sql="select * from user_table where UserName='"+UserName+"'and PassWord='"+Password+"'";
try{
rs=this.executeQuery(sql);
if(rs.next()){
boo=true;
}else{
boo=false;
}
this.closeConn();
}catch(SQLException se){
System.out.println (se.getMessage());
System.out.println ("操作表出错MyOdbc 58");
}
return boo;
}
public boolean loginEditor(String EditorName,String Password){
String sql="select * from EditorInfo_table where EditorName='"+EditorName+"' and Password='"+Password+"'";
try{
rs=this.executeQuery(sql);
if(rs.next()){
boo=true;
}else{
boo=false;
}
this.closeConn();
}catch(SQLException se){
System.out.println (se.getMessage());
System.out.println ("操作表出错MyOdbc 155");
}
return boo;
}
public void closeConn(){
try{
if(rs!=null){
rs.close();
System.out.println ("关闭rs");
}
if(stmt!=null){
stmt.close();
System.out.println ("关闭stmt");
}
if(conn!=null){
conn.close();
System.out.println ("关闭conn");
}
}catch(SQLException e){
System.out.println (e.getMessage());
}
}
public String getTime() {
String datestr = "" ;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-M-d HH:ss") ;
datestr = df.format(new java.util.Date()) ;
}
catch (Exception ex) {
System.out.println (ex.getMessage());
}
return datestr ;
}
}