JDBC
(转载http://blog.csdn.net/williamchew/article/details/51951551)
尊重版权所有
public class jdbc { public static final String DRIVER="org.gjt.mm.mysql.Driver"; public static void main(String[] args){ try{ System.out.println(Class.forName(DRIVER)) ; }catch(ClassNotFoundException e){ e.printStackTrace() ; } }} public static final String DRIVER="org.gjt.mm.mysql.Driver";public static final String URL="jdbc:mysql://localhost:3306/newsql";public static final String USERNAME="root"; Connection conn=null; Statement statement=null; String sql="insert into newtable(name) values(‘ccw‘)"; try{ Class.forName(DRIVER); //加载驱动 }catch(ClassNotFoundException e){ e.printStackTrace() ; } conn=DriverManager.getConnection(URL,USERNAME,USERNAME); statement=conn.createStatement(); statement.executeUpdate(sql); try{ statement.close(); //先开后关闭,可以只关闭connection conn.close(); }catch(Exception e){ e.printStackTrace(); }}ResultSet rSet=statement.executeQuery(sql);while(rSet.next()){ int id=rSet.getInt("id"); //int id=rSet.getInt(1); String name=rSet.getString("name"); //String name=rSet.getString(2); String sex=rSet.getString("sex"); //.... System.out.println(id+name+sex);}String sql="insert into newtable(name,sex) values(?,?)";pStatement=conn.prepareStatement(sql); //实例化pStatement.setString(1, name);pStatement.setString(2, sex);pStatement.executeUpdate(); String sql="select name,note from bigtable where id =?";pStatement=conn.prepareStatement(sql);pStatement.setInt(1, 1);ResultSet rs=pStatement.executeQuery();while(rs.next()){ String name=rs.getString(1); Clob clob=rs.getClob(2); String note=clob.getSubString(1, (int)clob.length()); System.out.println(name+" "+note);}String sql="insert into userblob(name, photo) values(?,?)";pStatement=conn.prepareStatement(sql);File file=new File("d:"+File.separator+"my.jpg");InputStream input=new FileInputStream(file);pStatement.setString(1, name);pStatement.setBinaryStream(2, input);pStatement.executeUpdate();if(rSet.next()){String name=rSet.getString(1);Blob blob=rSet.getBlob(2);FileOutputStream output=new FileOutputStream(newFile("d:"+File.separator+"you.jpg"));output.write(blob.getBytes(1,(int)blob.length())); output.close();} delimiter // //这里是改变执行操作语句的分隔符,也就是将SQL语句的";"结尾符号改为"//"drop procedure myproc //create procedure myproc(IN p1 int, INOUT p2 int ,OUT p3 int)begin select p1,p2,p3 ; set p1=10; set p2=20; set p3=30;end//String sql="{call myproc(?,?,?)}";CallableStatement cstmt=conn.prepareCall(sql);cstmt.setInt(1,70);cstmt.setInt(2,80);cstmt.registerOutParameter(2,Types.INTEGER); //设置返回值类型cstmt.registerOutParameter(3,Types.INTEGER);cstmt.execute();PreparedStatement pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL SENSITIVE,ResultSet.CONCUR_READ_ONLY); PreparedStatement pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATE);ResultSet rs=pstmt.executeQuery();rs.movetoInsertRow(); //移动到可插入的行rs.updateString("name","李华");...for(int i=0;i<10;i++){ ps.setString(1,"ccw"+i ); ps.setString(2, "nan"+i); ps.addBatch();} ps.executeBatch(); 原文:http://www.cnblogs.com/Cruyse/p/6926275.html