1.注册驱动Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动2.获取数据库连接java.sql.Connection conn=java.sql.DriverManager.getConnection(); 3获取表达式java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码 4执行SQLjava.sql.ResultSet rs=stmt.executeQuery("select * from user"); 5显示结果集里面的数据while(rs.next()){ System.out.println(rs.getInt(1)); System.out.pr ...
JDBC连接数据库(三种不同的数据库)(Oracle、SQLServer、MySQL)
关键字: java学习笔记 (jdbc)
JDBC编程步骤:
1. load the Driver
1. Class.forName()|Class.forName().newInstance()|newDriverName()
2.实例化时自动向DriverManager注册,不需显式调用DriverManager,registerDriver方法
2.Connect to the DataBase
1.DriverManager.getConnection()
3.Execute the SQL
1.Connection.CreateStatement()
2.Statement.executeQuery()
3.Statement.executeUpdate()
4.Retrieve the result date
1.循环取得结果while(rs.next)
5.Show the result data
1.将数据库种的各种类型转换为JAVA中的类型(getXXX)方法
6.Close
1.close the resultset./close the statement./close the connection
1.JDBC连接Oracle数据库示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//new oracle.jdbc.driver.OracleDriver();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
//请大家将上面的IP地址改为自己本机的IP地址;1521是端口号,大多数都是如此,SXT是数据库名称,改为自己的数据库名词,scott是用户名,tiger是密码。
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
2.JDBC连接SQLServer数据库示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=DBDEMO", "sa","123456");
// 与Oracle数据库类似首先加载驱动;上面1433为端口号,sa为用户名,123456为用户密码;
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
3.JDBC连接MySql示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DBDEMO", "root","root");
// 与上面数据库类似首先加载驱动;上面3306为端口号,DBDEMO为数据库,root为用户名,root为用户密码;
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
以上所用的数据库是自己建的一个数据库,名称为DBDEMO,里面定义了stuNo,stuName,stuSex,stuAge,stuAddresss,大家可以根据自己的需要用其他的数据库进行连接。
1. load the Driver
1. Class.forName()|Class.forName().newInstance()|newDriverName()
2.实例化时自动向DriverManager注册,不需显式调用DriverManager,registerDriver方法
2.Connect to the DataBase
1.DriverManager.getConnection()
3.Execute the SQL
1.Connection.CreateStatement()
2.Statement.executeQuery()
3.Statement.executeUpdate()
4.Retrieve the result date
1.循环取得结果while(rs.next)
5.Show the result data
1.将数据库种的各种类型转换为JAVA中的类型(getXXX)方法
6.Close
1.close the resultset./close the statement./close the connection
1.JDBC连接Oracle数据库示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//new oracle.jdbc.driver.OracleDriver();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
//请大家将上面的IP地址改为自己本机的IP地址;1521是端口号,大多数都是如此,SXT是数据库名称,改为自己的数据库名词,scott是用户名,tiger是密码。
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
2.JDBC连接SQLServer数据库示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=DBDEMO", "sa","123456");
// 与Oracle数据库类似首先加载驱动;上面1433为端口号,sa为用户名,123456为用户密码;
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
3.JDBC连接MySql示例:
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DBDEMO", "root","root");
// 与上面数据库类似首先加载驱动;上面3306为端口号,DBDEMO为数据库,root为用户名,root为用户密码;
stmt=conn.createStatement();
rs = stmt.executeQuery("select * from Stu_Info");
while(rs.next()){
System.out.println(rs.getString("StuNo"));
System.out.println(rs.getString("StuName"));
System.out.println(rs.getString("StuSex"));
System.out.println(rs.getString("StuAge"));
System.out.println(rs.getString("StuAddress"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
以上所用的数据库是自己建的一个数据库,名称为DBDEMO,里面定义了stuNo,stuName,stuSex,stuAge,stuAddresss,大家可以根据自己的需要用其他的数据库进行连接。