import java.sql.*;
public class Train {
static Connection con;
static Statement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=Text", "sa",
"123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
Train c = new Train();
con = c.getConnection();
try {
sql = con.createStatement();
res = sql.executeQuery("select * from tb_stu where name like '张%'");
while (res.next()) {
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String birthday = res.getString("birthday");
System.out.print("编号:" + id);
System.out.print(" 姓名:" + name);
System.out.print(" 性别:" + sex);
System.out.println(" 生日:" + birthday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}