62,615
社区成员
发帖
与我相关
我的任务
分享
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Testdb {
public static void main(String[] args) {
try {
//第一步:加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//第二步:建立连接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xinxin","root","xinxin");
//第三步:创建SQL语句
String SQL="select * from user";
//第四步:执行SQL语句
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(SQL);
//第五步:处理结果
while (rs.next()) {
System.out.print("用户ID:"+rs.getString(1)+" ");
System.out.print("用户名:"+rs.getString(2)+" ");
System.out.println("密码:"+rs.getString(3));
}
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}