mysql-java笔记
2014-05-24 13:23
204 查看
<pre name="code" class="java">import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class Test { private static Connection conn; public static void connect() throws SQLException { conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "danding"); } public static void listall(String tableName) throws SQLException { Statement statement = conn.createStatement(); statement.execute("select* from "+tableName); ResultSet res = statement.getResultSet(); ResultSetMetaData metadata = res.getMetaData(); int n=metadata.getColumnCount(); for (int i = 1; i <= n; i++) { System.out.print(metadata.getColumnName(i)+"\t"); } System.out.println(); while (res.next()) { for (int i = 1; i <= n; i++) { System.out.print(res.getObject(i)+"\t"); } System.out.println(); } } public ResultSet getResultSet(String query) throws SQLException { Statement statement = conn.createStatement(); statement.execute(query); ResultSet res = statement.getResultSet(); return res; } public void insertIFNotExists() throws SQLException { PreparedStatement preparedStatement = conn.prepareStatement("insert into s select ?,? from s " + "where not exists (select* from s where id=?)"); preparedStatement.setInt(1, 2); preparedStatement.setString(2,"李四"); preparedStatement.setInt(3, 2); preparedStatement.addBatch(); preparedStatement.setInt(1, 3); preparedStatement.setString(2,"王五"); preparedStatement.setInt(3, 3); preparedStatement.addBatch(); preparedStatement.executeBatch(); } public boolean checkDatabase(String name) throws SQLException { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost", "root", "danding"); DatabaseMetaData tt = connection.getMetaData(); ResultSet rs = tt.getCatalogs(); while (rs.next()) { if (rs.getString(1).equalsIgnoreCase(name)) { return true; } } return false; } public boolean checkTables(String name) throws SQLException { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "danding"); DatabaseMetaData tt = connection.getMetaData(); ResultSet rs = tt.getTables("test", null, null, new String[]{"TABLE"}); ResultSetMetaData metadata = rs.getMetaData(); System.out.println(metadata.getColumnCount()); while (rs.next()) { if (rs.getString("TABLE_NAME").equalsIgnoreCase(name)) { return true; } // for (int i = 1; i <= 10; i++) { // System.out.print(metadata.getColumnLabel(i)+" "); // } // System.out.println(); } return false; } public void run() { try { connect(); System.out.println(checkTables("s")); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub new Test().run(); } }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注意 "create database ?" 不能用预处理,还有就是select ? from table; 等类似的不能用预处理。insert into table1 values(?,?,?)这样可以</span>
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement("INSERT INTO TABLEX VALUES(?, ?)");
statement.setInt(1, 1);
statement.setString(2, "Cujo");
statement.addBatch();
statement.setInt(1, 2);
statement.setString(2, "Fred");
statement.addBatch();
statement.setInt(1, 3);
statement.setString(2, "Mark");
statement.addBatch();
int [] counts = statement.executeBatch();
connection.commit();//如果没有这一步connection.setAutoCommit(false);
可以不提交
出现这个错误Can not issue data manipulation statements with executeQuery().
意味着,插入,更新,删除的时候,不应该用statement.executeQuery(),这只有查询结果集的时候才用。改为statement.execute(sql)
相关文章推荐
- [Linux]在Linux上部署Java开发环境笔记(五):Linux中安装MySQL
- JAVA实训笔记(九)之mysql
- JAVA高级工程师课程笔记整理——(七)Mysql
- JAVA学习笔记-JAVA用JDBC连接MySQL
- mysql笔记七——Java实现excel表的读写(导出mysql数据库的所有表到excel表)
- 傻B自己看的java笔记:连接MYSQL
- 笔记:Java 创建txt文件并向其写入数据,再将数据存入到Mysql数据表
- java-mysql基本操作笔记2
- Linux java+tomcat+mysql+iptables笔记
- 【Java学习笔记】54:CentOS下安装MySQL的JDBC驱动并尝试连接
- Mysql Java驱动代码阅读笔记及JDBC规范笔记
- Java学习笔记之<JDBC连接MySQL的步骤>
- 21天学通Java学习笔记-Day12(MYsql-JDBC)
- Java_JDBC_MySQL学习笔记
- Java笔记---Mysql使用中的报错
- Java实训笔记(八)之mysql
- Java学习笔记——MySQL的安装使用以及SQL语法简介
- Linux+javaEE的学习笔记之MySQL的安装和备份、回复数据
- Java基础学习笔记二十四 MySQL安装图解
- JavaWeb-MySQL学习笔记