当前位置: 技术问答>java相关
200分求购JSP连接和操作数据库的原代码(JDK1。3+COMCAT4。0+SQLSERVER 2000)
来源: 互联网 发布时间:2015-04-05
本文导语: 如果有一定给加分,我分依次只能给这么多。WINDOWS NT 4。0 系统 | 我来也!给你个例子!改改用吧! /******************************************************** *@Linkdb v3.0 LINK JDBC AND DO SQL ...
如果有一定给加分,我分依次只能给这么多。WINDOWS NT 4。0 系统
|
我来也!给你个例子!改改用吧!
/******************************************************** *@Linkdb v3.0 LINK JDBC AND DO SQL *
*@AUTHOR:YuanLin *
*@UPDATE:Oct,21,2001 *
********************************************************/
//定义包opendb
package opendb;
//引入java.sql.*
import java.sql.*;
//包opendb中的类Linkdb
public class Linkdb
{
//调用数据库
//定义数据库连接字符串
/****************************************************************
//1. [ ORACLE ]
String sDBDriver="oracle.jdbc.driver.OracleDriver";
String sConnStr="jdbc:oracle:thin:@192.168.0.144:1521:server";
String userID="TEST";
String PassWord="TEST";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/*****************************************************************/
/***************************************************
//2. [ SQL Server ]
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:JSP";
String userID="TEST";
String PassWord="TEST";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
/***************************************************
//3. [ MY SQL ]
String sDBDriver="org.gjt.mm.mysql.Driver";
String sConnStr="jdbc:mysql://localhost/test";
//"jdbc:mysql://localhost/forum?user=root&password=admin"; (例子)
String userID="test";
String PassWord="";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
/***************************************************/
//4. [ ACCESS ] 参见JSPdevelop Page-231(使用JDBC-ODBC桥访问数据库)
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:testodbc";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
//定义打开数据库连接的函数
public Linkdb()
{
try{
//连接MySql_jdbc驱动程序
Class.forName(sDBDriver);}
//处理异常
catch(java.lang.ClassNotFoundException e){
System.err.println("Linkdb():"+e.getMessage());}
}//end method
//定义执行Select语句的函数
public ResultSet executeQuery(String sql){
rs=null;
try {
conn=DriverManager.getConnection(sConnStr);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
//处理异常
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}//end method
//执行Insert,Update语句
public void executeUpdate(String sql) {
stmt = null;
rs=null;
try {
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("执行SQL语句出错: " + ex.getMessage());
}
}//end method
//close Stmt
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}//end method
//close Conn
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}//end method
}//END CLASS
/******************************************************** *@Linkdb v3.0 LINK JDBC AND DO SQL *
*@AUTHOR:YuanLin *
*@UPDATE:Oct,21,2001 *
********************************************************/
//定义包opendb
package opendb;
//引入java.sql.*
import java.sql.*;
//包opendb中的类Linkdb
public class Linkdb
{
//调用数据库
//定义数据库连接字符串
/****************************************************************
//1. [ ORACLE ]
String sDBDriver="oracle.jdbc.driver.OracleDriver";
String sConnStr="jdbc:oracle:thin:@192.168.0.144:1521:server";
String userID="TEST";
String PassWord="TEST";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/*****************************************************************/
/***************************************************
//2. [ SQL Server ]
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:JSP";
String userID="TEST";
String PassWord="TEST";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
/***************************************************
//3. [ MY SQL ]
String sDBDriver="org.gjt.mm.mysql.Driver";
String sConnStr="jdbc:mysql://localhost/test";
//"jdbc:mysql://localhost/forum?user=root&password=admin"; (例子)
String userID="test";
String PassWord="";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
/***************************************************/
//4. [ ACCESS ] 参见JSPdevelop Page-231(使用JDBC-ODBC桥访问数据库)
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:testodbc";
Connection conn=null;
Statement stmt = null;
ResultSet rs=null;
/****************************************************/
//定义打开数据库连接的函数
public Linkdb()
{
try{
//连接MySql_jdbc驱动程序
Class.forName(sDBDriver);}
//处理异常
catch(java.lang.ClassNotFoundException e){
System.err.println("Linkdb():"+e.getMessage());}
}//end method
//定义执行Select语句的函数
public ResultSet executeQuery(String sql){
rs=null;
try {
conn=DriverManager.getConnection(sConnStr);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
//处理异常
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}//end method
//执行Insert,Update语句
public void executeUpdate(String sql) {
stmt = null;
rs=null;
try {
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("执行SQL语句出错: " + ex.getMessage());
}
}//end method
//close Stmt
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}//end method
//close Conn
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}//end method
}//END CLASS
|
1: sql server 2000 jdbc
Class forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection ("jdbc:microsoft:sqlserver://Myserver:1433;databasename=MyDB","username","password");
2:weblogic JDriver
Class forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
Connection conn=DriverManager.getConnection ("jdbc:webglogic:mssqlserver4:MyDB@Mysever:1433","username","password");
3.IDS Driver
Class forName("ids.sql.IDSDriver").newInstance();
Connection conn=DriverManager.getConnection ("jdbc:ids://Myserver:12/conn?dsn='MyDB',"username","password");
Class forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection ("jdbc:microsoft:sqlserver://Myserver:1433;databasename=MyDB","username","password");
2:weblogic JDriver
Class forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
Connection conn=DriverManager.getConnection ("jdbc:webglogic:mssqlserver4:MyDB@Mysever:1433","username","password");
3.IDS Driver
Class forName("ids.sql.IDSDriver").newInstance();
Connection conn=DriverManager.getConnection ("jdbc:ids://Myserver:12/conn?dsn='MyDB',"username","password");