JDBC連結MySql資料庫的測試Demo
阿新 • • 發佈:2019-01-01
package com.abc;
import java.sql.*;
public class MysqlDemo {
public static void main(String[] args){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/*****";
String user="root";
String password="******";
String name;
try{
Class.forName(driver);
Connection conn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from customer174";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
name = rs.getString("customer_name");
System.out.println(rs.getString("customer_city") + "\t" + name);
}
rs.close();
conn.close();
}
catch (ClassNotFoundException e) {
System.out.println("錯誤");
e.printStackTrace(); }
catch(SQLException e) {
e.printStackTrace(); }
catch(Exception e) {
e.printStackTrace();
}
}
}