myeclipse 10连接和查看数据库代码
展开全部
可以用jdbc来连接数据库,完整代码如下:
package com.jdbc.test;
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();
}
}
}
package com.jdbc.test;
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |