日期:2014-05-18  浏览次数:20780 次

为什么我连接数据库有错误
提示不明确的类:java.bean.Statement   和   java.sql.Statement
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
高手指点下,搞了好久了

------解决方案--------------------
Statement 用完整包名引用出来

例如:java.sql.Date java.Util.Date
------解决方案--------------------
要引入包
import java.sql.*;


普通代码:可以运行,但是我感觉有缺陷,你可以先运行试试

//mysqltest.java
package news;
import java.sql.*;
public class mysqltest
{
String sDBDriver = "org.gjt.mm.mysql.Driver ";
private static String url= "jdbc:mysql://10.164.80.52:3306/peidian?user=peidian&password=peidian-passwd&useUnicode=true&characterEncoding=gb2312 ";
private static Connection conn= null;
Statement stmt = null;
PreparedStatement pstmt=null;
ResultSet rs = null;

//getConn
public static Connection getConn()
{
try
{
if(conn==null)
conn = DriverManager.getConnection(url);
}
catch(Exception e)
{
System.err.println( "sql_data(): " + e.getMessage());
}
return conn;
}
//mysqltest
public mysqltest()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println( "sql_data(): " + e.getMessage());
}
}
//pstmt query
public PreparedStatement execute(String sql)
{
try
{
pstmt = getConn().prepareStatement(sql);
}
catch(SQLException ex)
{
System.err.println( "sql_data.executeUpdate: "+ex.getMessage());
}
return pstmt;
}
//insert
public void executeInsert(String sql)
{
try
{
stmt = getConn().createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println( "sql_data.executeUpdate: "+ex.getMessage());
}
}
//query
public ResultSet executeQuery(String sql)
{
try
{
stmt = getConn().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println( "sql_data.executeQuery: "+ex.getMessage());
}
return rs;
}
//update
public void executeUpdate(String sql)
{
try
{
stmt = getConn().createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println( "sql_data.executeUpdate: "+ex.getMessage());
}
}
//delete
public void executeDelete(String sql)
{
try
{
stmt = getConn().createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println( "sql_data.executeUpdate: "+ex.getMessage());
}
}
//close
public void closeDB()
{
try
{
if(stmt != null)
{
stmt.close();
stmt = null;
}
}
catch(SQLException e)
{
e.printStackTrace();
}
try
{
if(conn != null)
{
conn.close();
conn = null;
}
}