jdbc连接数据库,看main方法在哪里写?

2025-02-23 19:45:53
推荐回答(3个)
回答1:

JDBC(Java Data Base Connectivity)数据库连接,通常我们在编写web应用或java应用程序要连接数据库时就要使用JDBC。使用JDBC连接数据库一般步骤有:
1、加载驱动程序
Class.forName(driver);
2、创建连接对象
Connection con = DriverManager.getConnection(url,username,password);
3、创建sql语句执行对象
4、执行sql语句
5、对执行结果进行处理
6、关闭相关的连接对象(顺序跟声明的顺序相反)
下面是以建立与MySQL数据库连接的例子,其他数据库的过程类似:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection
{
public static void main(String[] args)
{
String driver = "com.mysql.jdbc.Driver";
//localhost指本机,也可以用本地ip地址代替,3306为MySQL数据库的默认端口号,“user”为要连接的数据库名
String url = "jdbc:mysql://localhost:3306/user";
//填入数据库的用户名跟密码
String username = "test";
String password = "test";
String sql = "select * from user";//编写要执行的sql语句,此处为从user表中查询所有用户的信息
try
{
Class.forName(driver);//加载驱动程序,此处运用隐式注册驱动程序的方法
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
Connection con = DriverManager.getConnection(url,username,password);//创建连接对象
Statement st = con.createStatement();//创建sql执行对象
ResultSet rs = st.executeQuery(sql);//执行sql语句并返回结果集
while(rs.next())//对结果集进行遍历输出
{
System.out.println("username: "+rs.getString(1));//通过列的标号来获得数据
System.out.println("useradd: "+rs.getString("useradd"));//通过列名来获得数据
System.out.println("userage: "+rs.getInt("userage"));
}
//关闭相关的对象
if(rs != null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(st != null)
{
try
{
st.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(con !=null)
{
try
{
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

回答2:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
// 最后的orcl是数据库库名
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
// 用户名
private static final String USER = "scott";
// 密码
private static final String PASSWORD = "tiger";

Connection conn;
Statement stmt;
ResultSet res;

public DBConnection() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}

public boolean getConnection() {
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
return true;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}

public int execUpdate(String sql) {
int rows = 0;

try {
stmt = conn.createStatement();
rows = stmt.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
}

return rows;
}

public ResultSet execResultSet(String sql) {
try {
stmt = conn.createStatement();
res = stmt.executeQuery(sql);
return res;
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}

public void close() {
if (res != null) {
try {
res.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}

}

public static void main(String[] args) {
DBConnection db = new DBConnection();
// 连接数据库
db.getConnection();
// 执行更新语句
String sql1 = "update table1 set name=\"123\"";
int num1 = db.execUpdate(sql1);
System.out.println("有" + num1 + "条记录更新完成");
// 执行查找语句
String sql2 = "select * from table1";
ResultSet resultSet = db.execResultSet(sql2);
try {
while (resultSet.next()) {
String name = resultSet.getString("name");
System.out.println("name=" + name);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 关闭数据库
db.close();
}
}

回答3:

public static void main(String[] args) {
DBConnection db = new DBConnection();
// 连接数据库
db.getConnection();
// 执行更新语句
String sql1 = "update table1 set name=\"123\"";
int num1 = db.execUpdate(sql1);
System.out.println("有" + num1 + "条记录更新完成");
// 执行查找语句
String sql2 = "select * from table1";
ResultSet resultSet = db.execResultSet(sql2);
try {
while (resultSet.next()) {
String name = resultSet.getString("name");
System.out.println("name=" + name);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 关闭数据库
db.close();
}