import com.MySQL.jdbc.Connection;
//连接数据库时首先要导入jdbc_mysql.jar驱动程序,可在网上下载,通过eclipse导入扩展jars文件导入开发项目
public class JDBCtest1 {public static void main(String[] args) throws SQLException,ClassNotFoundException {try {//载入jdbc-mysql 驱动
Class.forName("com.mysql.jdbc.Driver");// String url = "jdbc:mysql://localhost:3306/jdbctest";// String user = "root";// String passWord = "123456";//连接数据库Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123456");//生产Statement对象Statement stme = ((java.sql.Connection) conn).createStatement();//提交查询结果,返回结果集ResultSet rs = stme.executeQuery("Select * from student" ); //读取结果集 if (rs.next()) {System.out.PRint("学号:"+rs.getString(1)+" ");System.out.print("姓名:"+rs.getString(2)+" ");System.out.print("年龄:"+rs.getString(3));System.out.println();} else {System.out.println("数据库无数据");} rs.close(); stme.close(); conn.close();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println(e.getMessage());}catch (Exception e) {System.out.println("数据库不存在,链接出错");}}}新闻热点
疑难解答