package com.jdbc;
import java.sql.*;
public class jdbcdemo {
final static String JDBC_DRIVER="com.mysql.jdbc.Driver";
final static String DB_URL="jdbc:mysql://localhost/test";
final static String USER="root";
final static String PASS="*********";
public static void main(String[] args) throws ClassNotFoundException {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//加载数据库驱动
Class.forName(JDBC_DRIVER);
try {
//获得数据连接
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//验证下是否连接成功,成功则显示连接串信息
System.out.println(conn);
//执行SQL语句
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from tb4");
//显示SQL语句的结果
if(rs.next())
{
System.out.println("hello"+rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn!=null)
conn.close();
if(stmt!=null)
stmt.close();
if(rs!=null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Java的JDBC连接数据库
最新推荐文章于 2022-09-07 21:07:11 发布