连接数据库的源文件(重要):
package com.tjl.jdbc;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
public class JDBC {
public static void main(String[] args) {
//Drive loading
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Drive loading successful");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/data?characterEncoding=utf8",
"root", "240055");
//Database connection
System.out.println("Database connection successful");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT * FROM user");
while(result.next()) {
System.out.print(result.getInt("id") + " ");
System.out.print(result.getString("uname") + " ");
System.out.println(result.getString("upass"));
}
}catch (Exception e) {
e.printStackTrace();
System.out.println("Drive loading failed");
System.out.println("Database connection failed");
}
}
}
点赞
收藏
分享
文章举报
L嘉图.M.路
发布了14 篇原创文章 · 获赞 10 · 访问量 3143
私信
关注