java通过jdbc-odbc连接数据库
packagedatabase;importjava.sql.*;importjava.io.*;importjava.util.*;publicclassDatabas...
package database;
import java.sql.*;
import java.io.*;
import java.util.*;
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.*;
import java.io.*;
import java.util.*;
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;
}
} 展开
6个回答
展开全部
实现思路:就是通过ojdbc.jar中提供的方法,直接连接数据库即可,固定代码写法如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) throws Exception {
//1.加载驱动
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.创建数据库连接对象
//Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");
//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8","root","123456");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123");
//3.创建数据库命令执行对象
Statement stmt = conn.createStatement();
// PreparedStatement ps = conn.prepareStatement("select * from t_user");
//4.执行数据库命令
ResultSet rs = stmt.executeQuery("select * from t_user");
// ResultSet rs = ps.executeQuery();
//5.处理执行结果
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println(id + "\t" + username + "\t" + password);
}
//6.释放数据库资源
if (rs != null) {
rs.close();
}
// if (ps != null) {
// ps.close();
// }
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
展开全部
public Database()
{
}
这个去掉,把这写加上static
private static Connection conn = null;
private static ResultSet rs = null;
private static String drivername = "sun.JDBC.odbc.JdbcOdbcDriver";
private static String URL="JDBC:odbc:HotelManage";
public static Connection getConn()
public static ResultSet executeSQL(String str)
{
}
这个去掉,把这写加上static
private static Connection conn = null;
private static ResultSet rs = null;
private static String drivername = "sun.JDBC.odbc.JdbcOdbcDriver";
private static String URL="JDBC:odbc:HotelManage";
public static Connection getConn()
public static ResultSet executeSQL(String str)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的客户端程序怎么调这个Util类的..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询