当前位置: 技术问答>java相关
一个数据库操作javabean的问题
来源: 互联网 发布时间:2015-11-21
本文导语: package mydb; import java.sql.*; public class Dbconn { String sDBDriver="org.gjt.mm.mysql.Driver"; String sConnStr="'jdbc:mysql://localhost/sinomos?useUnicode=true&characterEncoding=UTF8','root','123456')"; Connection conn=null; ResultSet rs=null; ...
package mydb;
import java.sql.*;
public class Dbconn
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String sConnStr="'jdbc:mysql://localhost/sinomos?useUnicode=true&characterEncoding=UTF8','root','123456')";
Connection conn=null;
ResultSet rs=null;
public Dbconn()
{
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public void executeInsert(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
}
}
在JSP中引用
为什么总是写不进去?
import java.sql.*;
public class Dbconn
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String sConnStr="'jdbc:mysql://localhost/sinomos?useUnicode=true&characterEncoding=UTF8','root','123456')";
Connection conn=null;
ResultSet rs=null;
public Dbconn()
{
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public void executeInsert(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
}
}
在JSP中引用
为什么总是写不进去?
|
抛出什么异常?