一个经典的JDBC连接MySQL的程序
user = "root"; password = "yqs2602555";conn = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
System.out.println("数据库链接错误");
e.printStackTrace();
}
try {
stmt = conn.createStatement();
sql = "select * from dept";//dept这张表有deptno,deptname和age这三个字段
rs = stmt.executeQuery(sql);//执行sql语句
while(rs.next()) {
System.out.print(rs.getInt("deptno") + " ");
System.out.print(rs.getString("deptname") + " ");
System.out.println(rs.getInt("age") + " ");
}
} catch (SQLException e) {
System.out.println("数据操作错误");
e.printStackTrace();
}
页:
[1]