java编写的连接数据库的代码老是编译不过,求大神指导...........急!
packagedatabase;importjava.sql.*;publicclassDatabase{publicDatabase(){}privateConnect...
package database;
import java.sql.*;
public class Database
{
public Database()
{
}
private Connection conn = null;
private ResultSet rs = null;
private String drivername = "sun.JDBC.odbc.JdbcOdbcDriver";
private String URL="JDBC:odbc:HotelManage";
public Connection getConn()
{
try
{
Class.forName(drivername);
conn = DriverManager.getConnection(URL);
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
public ResultSet executeSQL(String str)
{
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(str);
}
catch (Exception e)
{
e.printStackTrace();
}
return rs;
}
} 展开
import java.sql.*;
public class Database
{
public Database()
{
}
private Connection conn = null;
private ResultSet rs = null;
private String drivername = "sun.JDBC.odbc.JdbcOdbcDriver";
private String URL="JDBC:odbc:HotelManage";
public Connection getConn()
{
try
{
Class.forName(drivername);
conn = DriverManager.getConnection(URL);
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
public ResultSet executeSQL(String str)
{
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(str);
}
catch (Exception e)
{
e.printStackTrace();
}
return rs;
}
} 展开
1个回答
展开全部
1 首先你在此java文件中没有定义main方法,定义main方法后再调用构造方法测试一下。
2 将private String drivername = "sun.JDBC.odbc.JdbcOdbcDriver"; 改为private String drivername=“sun.jdbc.odbc.JdbcOdbcDriver”; 将private String URL="JDBC:odbc:HotelManage";改为 private String URL="jdbc:odbc:HotelManage";试试看行不?
3 最好在Eclipse下测试,在命令行下该java文件的包路径会影响到该文件的编译和运行。
2 将private String drivername = "sun.JDBC.odbc.JdbcOdbcDriver"; 改为private String drivername=“sun.jdbc.odbc.JdbcOdbcDriver”; 将private String URL="JDBC:odbc:HotelManage";改为 private String URL="jdbc:odbc:HotelManage";试试看行不?
3 最好在Eclipse下测试,在命令行下该java文件的包路径会影响到该文件的编译和运行。
追问
main应该怎么定义,我不会.........
追答
public static void main(String args[])
{
String sql="";
Database database = new Database();
Connection connection = database.getConnection();
ResultSet resultset = database.executeSQL(sql);
while(resultset.next())
{
//显示查询结果
}
}
建议你先看看为何这样实现,再看代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询