作者在 2018-03-26 14:56:01 发布以下内容
package sjk_1;
import java.sql.*;
public class sjkmysql_1 {
public static void main(String[] args){
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/usb";
String user="root";
String password="root";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("—————————————");
System.out.println("姓名"+"\t"+"职称");
Statement statement = con.createStatement();
String sql = "select * from emp";
ResultSet rs = statement.executeQuery(sql);
System.out.println("—————————————");
String job = null;
String id = null;
while(rs.next()){
job = rs.getString("job");
id = rs.getString("ename");
System.out.println(id + "\t" + job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}
import java.sql.*;
public class sjkmysql_1 {
public static void main(String[] args){
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/usb";
String user="root";
String password="root";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("—————————————");
System.out.println("姓名"+"\t"+"职称");
Statement statement = con.createStatement();
String sql = "select * from emp";
ResultSet rs = statement.executeQuery(sql);
System.out.println("—————————————");
String job = null;
String id = null;
while(rs.next()){
job = rs.getString("job");
id = rs.getString("ename");
System.out.println(id + "\t" + job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}