package cn.gzsxt.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestStatementSQLinsert {
public static void main(String[] args) {
// SQL注入问题
String u = "zhangsan";
String p = "123' or '1' = '1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.获取数据库连接对象
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "SCOTT";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from t_user where username = '" + u + "' and password = '" + p + "'";
// 执行该sql语句
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("username")+"-->"+rs.getString("password"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package cn.gzsxt.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestStatement {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "SCOTT";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
// 3.通过connection对象获取一个Statement对象,用于将sql语句发送到数据库
Statement stmt = conn.createStatement();
// executeQuery() 只能用在select关键字的查询中
// testExecuteQuery(stmt);
// executeUpdate() 用于执行DML语句或DDL语句
// testExecuteUpdate(stmt);
// stmt.execute(sql)
testExecute(stmt);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("请检查sql驱动类是否正确");
} catch (SQLException e) {
e.printStackTrace();
// System.out.println("请检查数据库地址,用户名,密码是否正确");
}
}
private static void testExecute(Statement stmt) throws SQLException {
//execute方法可以执行任意的sql语句
String sql1 = "select e.ename,e.empno from emp e";
boolean flag = stmt.execute(sql1);
ResultSet rs = stmt.getResultSet();
System.out.println(flag);
String sql2 = "update emp set sal = sal*1.1 where deptno = 20";
boolean flag2 = stmt.execute(sql2);
int result = stmt.getUpdateCount();
}
private static void testExecuteUpdate(Statement stmt) throws SQLException {
/*String sql = "insert into emp(empno,ename,sal) values(4433,'laowang',2000)";
int result = stmt.executeUpdate(sql);
System.out.println(result);*/
//给20部门员工涨工资:10%
String sql = "update emp set sal = sal*1.1 where deptno = 20";
int result = stmt.executeUpdate(sql);
System.out.println(result);
//利用该方法创建一张表 t_testtable
}
public static void testExecuteQuery(Statement stmt) throws SQLException {
String sql = "select e.ename,e.empno from emp e";
// 4.获取结果集对象
ResultSet rs = stmt.executeQuery(sql);
// 5.遍历结果集
while (rs.next()) {
//System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
System.out.println( rs.getInt("empno")+"\t"+rs.getString("ename"));
}
}
}
package cn.gzsxt.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestPreparedStatement {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// SQL注入问题
String u = "zhangsan";
String p = "123456";
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.获取数据库连接对象
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "SCOTT";
String password = "tiger";
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from t_user where username = ? and password = ?";
ps = conn.prepareStatement(sql);
// ps对象有一系列的set方法
ps.setString(1, u);
ps.setString(2, p);
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("username") + "-->" + rs.getString("password"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.gzsxt.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJDBCgetTable {
public static void main(String[] args) {
try {
// 1.加载数据库驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.获取数据库连接对象
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "SCOTT";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
// 3.获取数据库的元信息
DatabaseMetaData metaData = conn.getMetaData();
// 查看数据库中有哪些表
// ResultSet该对象类似于迭代器
ResultSet rs = metaData.getTables(conn.getCatalog(), "SCOTT", null, new String[] { "TABLE", "VIEW" });
while (rs.next()) {
System.out.println(rs.getString(2) + "-->" + rs.getString(3));
}
// 查看数据库的emp表有哪些列
ResultSet rs2 = metaData.getColumns(conn.getCatalog(), "SCOTT", "EMP", null);
while(rs2.next()) {
System.out.println(rs2.getString(4)+"-->"+rs2.getString(6)+"-->"+rs2.getInt(7));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}