java使用jdbc方式连接数据库实例

时间:2022-12-23 13:05:29

连接sqlserver数据库

package jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTest1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//加载jdbc驱动,Class.forName()加载
try {
Class.forName(className );//////////////////////////11111111111111
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Driver driver = null;

///注册jdbc驱动,在DriverManager类中注册,registerDriver(Driver driver)
try {
driver = new com.microsoft.sqlserver.jdbc.SQLServerDriver();///////////////////////////////////
DriverManager.registerDriver(driver );//////////////////////////////////222222222222
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//3建立数据库连接Connection对象,由DriverManager建立连接对象,知道连接什么数据库、连接用户名和密码
Connection con = null;
String url ="jdbc:sqlserver://localhost:1433;DatabaseName=db";//jdbc url,不同数据库的识别方法是不一样的。
String user ="sa";
String password ="123456";
try{
con = DriverManager.getConnection(url, user, password);//////////////////////////////////////
}catch(Exception e){
e.printStackTrace();

}


//PreparedStatement
PreparedStatement pstmt = null;
String sql = "select * from book where price <? and number > ?";
try {
pstmt = con.prepareStatement(sql );////////////////////////////
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pstmt.setFloat(1, (float) 50.0);
pstmt.setInt(2, 5);
} catch (SQLException e) {
e.printStackTrace();
}


//借助于语句对象,执行数据库sql操作


ResultSet rs = null;
try {
rs = pstmt.executeQuery();////////////////////////////////////////
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

///处理数据库查询结果
try {
while(rs.next()){
String isbn = rs.getString(1);
String author = rs.getString("author");
Float price = rs.getFloat("price");
Integer number = rs.getInt("number");
System.out.println("isbn:"+isbn+"\nauthor:"+author+"\ntotal:"+price*number+"\n");
}
} catch (SQLException e) {
e.printStackTrace();
}
///关闭资源,与开启顺序相反
try{

if(rs!=null){
rs.close();

}
if(pstmt!=null){
pstmt.close();

}
if(con!=null){
con.close();

}
}catch(SQLException e){
e.printStackTrace();

}
}

}

连接mysql数据库

package jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {

/**查询数据库表中的内容,并在控制台输出显示
* @param args
*/
public static void main(String[] args) {

String className = "com.mysql.jdbc.Driver";
//加载jdbc驱动,Class.forName()加载
try {
Class.forName(className );//////////////////////////11111111111111
} catch (ClassNotFoundException e) {
e.printStackTrace();
}


Driver driver = null;

///注册jdbc驱动,在DriverManager类中注册,registerDriver(Driver driver)
try {
driver = new com.mysql.jdbc.Driver();///////////////////////////////////
DriverManager.registerDriver(driver );//////////////////////////////////222222222222
} catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
}


//3建立数据库连接Connection对象,由DriverManager建立连接对象,知道连接什么数据库、连接用户名和密码
Connection con = null;
String url ="jdbc:mysql://localhost:3306/db";//jdbc url,不同数据库的识别方法是不一样的。
String user ="root";
String password ="123456";
try{
con = DriverManager.getConnection(url, user, password);//////////////////////////////////////
}catch(Exception e){
e.printStackTrace();

}


////4...准备操作数据库前的工作,借助于Statement或PreparedStatement来操作数据库,可以通过Connection对象得到语句对象

//Statement
Statement stmt = null;
try {
stmt = con.createStatement();///////////////////////////
} catch (SQLException e) {
e.printStackTrace();
}

/**PreparedStatement
PreparedStatement pstmt = null;
String sql = "select * from book where price <? and number > ?";
try {
pstmt = con.prepareStatement(sql );////////////////////////////
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pstmt.setFloat(1, (float) 50.0);
pstmt.setInt(2, 5);
} catch (SQLException e) {
e.printStackTrace();
}**/


//借助于语句对象,执行数据库sql操作

String sql = "select * from book where price <50 and number>5";
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);////////////////////////////////////////
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

///处理数据库查询结果
try {
while(rs.next()){
String isbn = rs.getString(1);
String author = rs.getString("author");
Float price = rs.getFloat("price");
Integer number = rs.getInt("number");
System.out.println("isbn:"+isbn+"\nauthor:"+author+"\ntotal:"+price*number+"\n");
}
} catch (SQLException e) {
e.printStackTrace();
}

///关闭资源,与开启顺序相反
try{

if(rs!=null){
rs.close();

}
if(stmt!=null){
stmt.close();

}
if(con!=null){
con.close();

}
}catch(SQLException e){
e.printStackTrace();

}

}

}