您的位置:首页 > 数据库 > MySQL

JDBC操作mysql

2015-10-02 21:52 519 查看
package pack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.*;
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 java.sql.Types;

import com.mysql.jdbc.CallableStatement;
import java.sql.*;
public class Main {

static void test() throws SQLException {
//1.注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//或:System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
//或:Class.forName("com.mysql.jdbc.Driver");

//2.建立连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

//3.创建语句
Statement st = conn.createStatement();

//4.执行语句
ResultSet rs = st.executeQuery("select * from class");

//5.处理结果
while(rs.next()) {
System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)
+"\t"+rs.getObject(3)+"\t"+rs.getObject(4));
}

//6.释放资源
rs.close();
st.close();
conn.close();
}

static void test1() throws SQLException { //增删改查
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");
Statement st = conn.createStatement();

//String sql = "insert into class values(5,'haha',1,78)";
//String sql = "update class set goal=goal+1 where id=5";
String sql = "select id from class where name='' or 1"; //sql注入,全选
st.executeUpdate(sql);
}

static void test2() throws SQLException { //利用PreparedStatement解决sql注入问题,可以指定sql语句变量
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

PreparedStatement ps = conn.prepareStatement("select goal from class where name=?");
//ps.setString(1,"'' or 1" ); //1代表第一个问号
ps.setString(1,"Alice" ); //1代表第一个问号
ResultSet rs = ps.executeQuery();

while(rs.next()) {
System.out.println(rs.getObject("goal"));
}
}

static void test3() throws SQLException, IOException { //写入大文本数据
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

PreparedStatement ps = conn.prepareStatement("insert into file(text) values(?)");
File file = new File("E:\\html\\1.txt");
Reader reader = new BufferedReader(new FileReader(file));
ps.setCharacterStream(1, reader, (int)file.length());
ps.executeUpdate();
reader.close();
}

static void test4() throws SQLException, IOException { //读取大文本数据
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

PreparedStatement ps = conn.prepareStatement("select text from file where id=6");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
Clob cl = rs.getClob(1); //获取大文本用的类型

Reader reader = rs.getClob(1).getCharacterStream();//转换为文本流
char[] buf = new char[1024];
FileWriter fw = new FileWriter("E:\\html\\2.txt");
while(reader.read(buf)!=-1) {
fw.write(buf, 0, buf.length);
}

fw.close();
reader.close();
}
}

static void test5() throws SQLException, IOException {  //事务(保证原子性)
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

conn.setAutoCommit(false);  //打开事务

st = conn.createStatement();
st.executeUpdate("update class set goal=goal-1 where id=1");
rs = st.executeQuery("select goal from class where id=2");
while(rs.next()) {
if(rs.getFloat("goal")>100)
throw new RuntimeException("over flow");
}
st.executeUpdate("update class set goal=goal+1 where id=2");

conn.commit();  //提交事务
} catch (SQLException e) {
if(conn!=null)
conn.rollback();  //回滚事务(撤销事务内操作)
throw e;
}
}

static void test6() throws SQLException, IOException {  //事务(保证原子性)
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Savepoint sp = null;   //保存点,回滚不撤销所有操作
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

conn.setAutoCommit(false);  //打开事务

st = conn.createStatement();
st.executeUpdate("update class set goal=goal-1 where id=1");

sp = conn.setSavepoint(); //设置保存点

rs = st.executeQuery("select goal from class where id=2");
while(rs.next()) {
if(rs.getFloat("goal")>100)
throw new RuntimeException("over flow");
}
st.executeUpdate("update class set goal=goal+1 where id=2");

conn.commit();  //提交事务
} catch (SQLException e) {
if(conn!=null && sp!=null) {
conn.rollback(sp);  //回滚事务(撤销事务内操作)
conn.commit();
}
throw e;
}

}

static void test7() throws SQLException { //调用存储过程
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");
java.sql.CallableStatement cs = conn.prepareCall("{call p(?,?,?,?}");
cs.registerOutParameter(4, Types.INTEGER);
cs.setInt(1, 5);
cs.setString(2, "min");
cs.setFloat(3, 97);
cs.executeUpdate();
int id = cs.getInt(4);
System.out.println(id);

}

static void test8() throws SQLException, IOException {  //批处理
Connection conn = null;
Statement st = null;
ResultSet rs = null;

DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

st = conn.createStatement();
st.addBatch("insert into class values(5,'mm',67)");
st.addBatch("insert into class values(6,'hj',67)");
st.addBatch("insert into class values(7,'hg',67)");
st.executeBatch();
st.close();

}

static void test9() throws SQLException, IOException {   //可滚动结果集
Connection conn = null;
Statement st = null;
ResultSet rs = null;

DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);

rs = st.executeQuery("select * from class");
while(rs.next()) {
System.out.println(rs.getInt(1));
}
rs.absolute(2);//游标指定第2行
rs.getRow();//一共多少条记录
while(rs.previous()) {
System.out.println(rs.getInt(1));
}
}

static void test10() throws SQLException, IOException {   //可更新结果集
Connection conn = null;
Statement st = null;
ResultSet rs = null;

DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

rs = st.executeQuery("select * from class");
rs.next();

//更新一行数据
rs.updateFloat(3, 11);
rs.updateRow(); //保存到数据库

//插入新行
rs.moveToInsertRow();
rs.updateString(2, "aa");
rs.updateFloat(3, 77);
rs.insertRow();

//将光标移到新建的行
rs.moveToCurrentRow();

//删除行
rs.absolute(2);
rs.deleteRow();

//取消更新
rs.cancelRowUpdates();
}

static void test11() throws SQLException, IOException {   //数据库元数据信息
Connection conn = null;
PreparedStatement ps = null;
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","111111");

/*DatabaseMetaData md= conn.getMetaData();  //数据库元数据信息
System.out.println(md.getDatabaseProductName());//mysql*/

ps = conn.prepareStatement("select * from class where id=2");
ParameterMetaData pmd = ps.getParameterMetaData();  //参数元数据信息
int count = pmd.getParameterCount();
for(int i=1; i<=count; i++) {
System.out.println(pmd.getParameterType(i));
System.out.println(pmd.getParameterClassName(i));
System.out.println(pmd.getParameterTypeName(i));
}
}

public static void main(String[] args) throws SQLException, IOException {
//test();
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
//test8();
//test9();
test11();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql jdbc