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

import com.mysql.jdbc.Driver;

public class Sqltest {

private final static String DRIVER="com.mysql.jdbc.Driver";
private final static String URL = "jdbc:mysql://127.0.0.1:3306/signin";
private final static String USERNAME = "root";
private final static String PASSWORD = "21424019";
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Driver driver = (Driver)Class.forName(DRIVER).newInstance();
DriverManager.registerDriver(driver);
Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
con.setAutoCommit(false);
//String sql="select user_id from `test`.`new_table` where user_id=";
String sql="insert into test.new_table(user_id,password) values(?,?)";
String sql2=" and password=";
String user_id1="harry1",password1="123456";
String user_id2="'potter1' or '1'='1'--";
String password2="'23456790'";
StringBuffer sb=new StringBuffer();
sb.append(sql);
sb.append(user_id1);
sb.append(sql2);
sb.append(password1);
PreparedStatement preparestatement = con.prepareStatement(sql);
preparestatement.setString(1,user_id1);
preparestatement.setString(2, password1);
Savepoint svpt=con.setSavepoint();
int lines=preparestatement.executeUpdate();
if(lines>=1)
{
System.out.println(lines);
con.rollback();
//con.rollback(svpt);
}

con.commit();

con.releaseSavepoint(svpt);
/*Statement statement = con.createStatement();
System.out.println("sql: "+sb.toString());
ResultSet result= statement.executeQuery(sb.toString());
while(result.next())
{
System.out.println("USER_ID1");
System.out.println(result.getString(1));
}
sb.setLength(0);
sb.append(sql);
sb.append(user_id2);
sb.append(sql2);
sb.append(password2);
ResultSet result2 = statement.executeQuery(sb.toString());
while(result2.next())
{
System.out.println("USER_ID2");
System.out.println(result2.getString(1));
}*/
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

java sql的更多相关文章

  1. Caused by: java.sql.BatchUpdateException: Transaction error, need to rollback. errno:1205 Lock wait timeout exceeded; try restarting transaction

    更新的时候报 Caused by: java.sql.BatchUpdateException: Transaction error, need to rollback. errno:1205 Loc ...

  2. '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error

    '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error 异常现象 ### Cause: java.sql.SQ ...

  3. java.sql.Connection解决插入数据库中文乱码问题

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public clas ...

  4. java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to repair it

    java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to re ...

  5. mysql解决Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp

    同步发布:http://www.yuanrengu.com/index.php/mysqlsolvetimestamp.html 在使用mysql时,如果数据库中的字段类型是timestamp,默认为 ...

  6. java.sql.SQLException: ORA-00911: invalid character 解决方法

    java.sql.SQLException: ORA-00911: invalid character 控制台抛出这个异常:java.sql.SQLException: ORA-00911: inva ...

  7. java.sql.SQLException: No suitable driver 问题解决

    最近在学习java,用到c3p0数据库连接池,遇到一个很奇怪的现象,用main方法测试是可以正常连接数据库的,但是使用jsp调用代码,就会报如下图的错误! 最下面的java.sql.SQLExcept ...

  8. java.sql.SQLException: JZ00L

    出现, java.sql.SQLException: JZ00L: 登录失败.检查与此异常现象有关的 SQL 警告以获得失败原因. at com.sybase.jdbc3.jdbc.ErrorMess ...

  9. Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement

    Connection.prepareStatement()函数出错,提示: Type mismatch: cannot convert from java.sql.PreparedStatement ...

  10. java.util.Date与java.sql.Date

    我数据库里用到了日期类型.用java编程的时候同时import了java.util.*和java.sql.*,发现直接申明Date类型 Date dt; 会报错,查了一下才发现有java.util.D ...

随机推荐

  1. Spring Data 整合 ElasticSearch搜索服务器

    一.基于 maven 导入坐标(pom.xml文件) <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  2. select控件自动触发change事件

    这里接上面的二级联动.背景:当页面跳转到修改页面时,需要首先绑定学院和专业.这就需要在页面加载时触发select的change事件,具体用trigger函数进行实现.代码如下. $("#xs ...

  3. 【NOIP2014提高组】联合权值

    https://www.luogu.org/problem/show?pid=1351 既然是一棵树,就先转化成有根树.有根树上距离为2的点对,路径可能长下面这样: 枚举路径上的中间点X. 第一种情况 ...

  4. 大数据学习系列之五 ----- Hive整合HBase图文详解

    引言 在上一篇 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机) 和之前的大数据学习系列之二 ----- HBase环境搭建(单机) 中成功搭建了Hive和HBase的环 ...

  5. 程序员的自我救赎---1.4.3: 核心框架讲解(MVC)

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  6. Hadoop之SequenceFile

    Hadoop序列化文件SequenceFile能够用于解决大量小文件(所谓小文件:泛指小于black大小的文件)问题,SequenceFile是Hadoop API提供的一种二进制文件支持.这样的二进 ...

  7. UVA 540(队列)

    Description  Team Queue  Queues and Priority Queues are data structures which are known to most comp ...

  8. dma_alloc_coherent (建立一致性 DMA 映射函数)

    1.函数申明 /** * dma_alloc_coherent - allocate consistent memory for DMA * @dev: valid struct device poi ...

  9. android.app.Activity 的介绍

    发现当前Android的资料不是非常多,并且对于Activity的介绍也非常少.所以把官方文档的android.app.Activity的介绍翻译了一下,增加了一些自己的理解.各位假设认为我自己理解的 ...

  10. 你懂AI吗(1)

    那场载入史册的战争之后,AI成为地球的主人已经快一个世纪了. 随着见证这场战争的那一代人基本消失,除了几个要堵上人类的尊严,颠覆AI的邪恶统治的组织外,现在的人基本已经习惯了这个AI统治的世界. AI ...