当前位置: 技术问答>java相关
JDBC-NET纯JAVA
来源: 互联网 发布时间:2014-12-28
本文导语: 请举个例子说明如何装载连接JDBC-NET纯JAVA?有什么准备工作,需要那些文件?请具体的讲讲。 还有一个问题是JDK1。3包含了那些JDBC的DRIVER,属于那四种的那一种?都是那些? 请谈谈。。。。 | 我用的是...
请举个例子说明如何装载连接JDBC-NET纯JAVA?有什么准备工作,需要那些文件?请具体的讲讲。
还有一个问题是JDK1。3包含了那些JDBC的DRIVER,属于那四种的那一种?都是那些?
请谈谈。。。。
还有一个问题是JDK1。3包含了那些JDBC的DRIVER,属于那四种的那一种?都是那些?
请谈谈。。。。
|
我用的是mysql,驱动包从mysql站点下载,具体例程如下:,示例如下:
set CLASSPATH=e:jdklibTools.jar;e:jdklibdt.jar;.;e:myjspserver;e:jakarta-tomcatlibservlet.jar;e:jdkdrivermysql.jar;
数据库连接bean:
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";//数据库驱动
String ConnStr = "jdbc:mysql://localhost:3306/net";//net为数据库名
Connection conn = null;
public conn() {
try
{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException error)
{
System.err.println(error.getMessage());
}
}
public Connection getconn() {
try
{
conn = DriverManager.getConnection(ConnStr,"","");
//用户和口令为空
return conn;
}
catch (SQLException errorc)
{
System.err.println(errorc.getMessage());
return null;
}
}
public void close() {
try {
conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
返回记录集bean
package mydb;
import java.sql.*;
public class result
{
Connection m_conn = null;
Statement m_stmt = null;
ResultSet m_rs = null;
public result() {
}
public void setconn(Connection conn) {
m_conn=conn;
try {
m_stmt = m_conn.createStatement();
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
m_rs = m_stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
return m_rs;
}
public void executeUpdate(String sql) {
try {
m_stmt.executeUpdate(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public void close() {
try {
m_rs.close();
m_stmt.close();
m_conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
调用示例:
Common Database Bean Test
set CLASSPATH=e:jdklibTools.jar;e:jdklibdt.jar;.;e:myjspserver;e:jakarta-tomcatlibservlet.jar;e:jdkdrivermysql.jar;
数据库连接bean:
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";//数据库驱动
String ConnStr = "jdbc:mysql://localhost:3306/net";//net为数据库名
Connection conn = null;
public conn() {
try
{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException error)
{
System.err.println(error.getMessage());
}
}
public Connection getconn() {
try
{
conn = DriverManager.getConnection(ConnStr,"","");
//用户和口令为空
return conn;
}
catch (SQLException errorc)
{
System.err.println(errorc.getMessage());
return null;
}
}
public void close() {
try {
conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
返回记录集bean
package mydb;
import java.sql.*;
public class result
{
Connection m_conn = null;
Statement m_stmt = null;
ResultSet m_rs = null;
public result() {
}
public void setconn(Connection conn) {
m_conn=conn;
try {
m_stmt = m_conn.createStatement();
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
m_rs = m_stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
return m_rs;
}
public void executeUpdate(String sql) {
try {
m_stmt.executeUpdate(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public void close() {
try {
m_rs.close();
m_stmt.close();
m_conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
调用示例:
Common Database Bean Test