package jdbc.com.test;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public static void main(String[] args) throws SQLException,ClassNotFoundException {
try {
// 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 block
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println("数据库不存在,链接出错");
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 block
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println("数据库不存在,链接出错");
}
}
}