我写的一个bean
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";
String ConnStr = "jdbc:mysql://localhost:3306/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());
}
}
}
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";
String ConnStr = "jdbc:mysql://localhost:3306/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());
}
}
}
解决方案 »
- 简单的JAVA问题,麻烦高手来看看。
- 问个关于键盘监听的小问题!
- 迪米特法则相关
- 如何用JAVA在本地播放音乐??
- jTextPane设置为text/html后,我直接将WORD中的文档复制进去,无法成功,怎么办?
- 如何得到字符的ascii码?
- 概念问题:JAVA是代表全部嘛?
- 如何让Applet在网页中能够被调节大小?
- 新手问题!
- 有用过textpad的吗?
- 一个关于传递参数的问题?
- 为什么IBM的domOne例程不能运行?编译时出现Note: 5 files use or override a deprecated API. Recompile with "-deprecation" for details.错误!
package dbbean;
import java.sql.*;
import java.io.*;
public class dbbean{
String sDBDriver="oracle.jdbc.driver.OracleDriver";
Connection conn=null;
ResultSet rs=null;
//构造函数
public dbbean(){
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("dbbean(): "+e.getMessage());
}
} public ResultSet executeQuery(String sqlStr){
rs=null;
try{
conn=
DriverManager.getConnection"jdbc:oracle:thin:@lmz:1521:labora",
"szfao","szfao");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlStr);
}catch(SQLException e){
System.err.println("executeQuery(): "+e.getMessage());
}
return rs;
} public int executeInsert(String sqlStr){
try{
conn=DriverManager.getConnection
("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
return 1;//成功
}catch(SQLException e){
System.err.println("executeInsert(): "+e.getMessage());
return 0;//失败
}
} public int executeUpdate(String sqlStr){
try{
conn=DriverManager.getConnection
("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
return 1;//成功
}catch(SQLException e){
System.err.println("executeInsert(): "+e.getMessage());
return 0;//失败
}
} public int executeDelete(String sqlStr){
try{
conn=DriverManager.getConnection
("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
return 1;//成功
}catch(SQLException e){
System.err.println("executeDelete(): "+e.getMessage());
return 0;//失败
}
}
}