快速入手Jdbc并实现最简单的demo
1.jdbc操作oracle数据库
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.1</version>
</dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo {
public void getDataList() {
Connection connection=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbc:oracle:thin:(这里写自己oracle服务器的ip地址):orcl","airquality","airquality");
String sql="select name from POIS_NEW where rownum < 10";
stmt=connection.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
String NAME = rs.getString("name");
System.out.println(NAME);
}
rs.close();
stmt.close();
connection.close();
System.out.println("JdbcDemo1.main()");
} catch (Exception e) {
}
}
public static void main(String[] args) {
JdbcDemo jdbcDemo=new JdbcDemo();
jdbcDemo.getDataList();
}
}
2.jdbc操作mysql数据库
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo1 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jt_db?characterEncoding=utf-8",
"root", "root");
Statement stat = conn.createStatement();
String sql = "select * from account";
ResultSet rs = stat.executeQuery(sql);
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
System.out.println(
id+" : "+name+" : "+money );
}
rs.close();
stat.close();
conn.close();
System.out.println("JdbcDemo1.main()");
}
}
3.jdbc的crud(数据库用的是mysql)
直接上代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
public static Connection getConn() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql:///jt_db?characterEncoding=utf-8",
"root", "root");
return conn;
}
public static void close(
Connection conn, Statement stat, ResultSet rs) {
if( rs != null ) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
if( stat != null ) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
stat = null;
}
}
if( conn != null ) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.tedu.util.JdbcUtil;
public class JdbcCRUD {
@Test
public void add( ) {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConn();
stat = conn.createStatement();
String sql = "insert into account values "
+ "(null, 'john', 3000)";
int rows = stat.executeUpdate( sql );
System.out.println("影响行数: "+rows);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, stat, rs);
}
}
@Test
public void update( ) {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConn();
stat = conn.createStatement();
String sql = "update account set money=1500"
+ " where name='john' ";
int rows = stat.executeUpdate( sql );
System.out.println( "影响行数: "+rows );
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, stat, rs);
}
}
@Test
public void delete( ) {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConn();
stat = conn.createStatement();
String sql = "delete from account where "
+ "name='john'";
int rows = stat.executeUpdate( sql );
System.out.println( "影响行数: "+rows );
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, stat, rs);
}
}
@Test
public void findById( ) {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConn();
stat = conn.createStatement();
String sql = "select * from account where id=1";
rs = stat.executeQuery( sql );
if(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
System.out.println(id+" : "+name+" : "+money);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.后续说明
- 连接池的使用
- 传输器对象PreparedStatement的使用(防止sql注入攻击)
- 最后别忘了释放资源