package org.chongwu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class master {
// 数据库的用户名与密码,需要根据自己的设置
static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dbURL="jdbc:sqlserver://localhost:3306:DatabaseName=Pet";
static final String USER = "root";
static final String PASS = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(driverName);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(dbURL,USER,PASS);
// 执行查询
System.out.println("实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT 宠物主人姓名,宠物主人年龄,宠物主人性别,宠物姓名,宠物年龄,宠物品种 FROM master";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String a = rs.getString("宠物主人姓名");
String b = rs.getString("宠物主人年龄");
String c = rs.getString("宠物主人性别");
String d = rs.getString("宠物姓名");
String e = rs.getString("宠物年龄");
String f = rs.getString("宠物品种");
System.out.print("宠物主人姓名:"+a);
System.out.print(",宠物主人年龄:"+b);
System.out.print(",宠物主人性别:"+c);
System.out.print(",宠物姓名:"+d);
System.out.print(",宠物年龄:"+e);
System.out.print(",宠物品种:"+f);
System.out.print("\n");
}
rs.close();stmt.close();conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}