import java.sql.*; String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost/test"; // Database credentials String USER = "root"; String PASS = "root"; Connection conn = null; Statement stmt = null; String time,id,bytes,file,access,user,service,state; try{ conn = DriverManager. getConnection(DB_URL,USER,PASS); stmt = conn.createStatement(); String sql; sql = "SELECT time, id, bytes, file, access, user, service, state FROM DOWNLOAD_RECORD"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { //Retrieve by column name time = rs.getString( "time"); id = rs.getString( "id"); bytes= rs.getString( "bytes"); file = rs.getString( "file"); access = rs.getString( "access"); user = rs.getString( "user"); service = rs.getString( "service"); state = rs.getString( "state"); System. out.println(time); System. out.println(id); System. out.println(bytes); System. out.println(file); System. out.println(access); System. out.println(user); System. out.println(service); System. out.println(state); } rs.close(); if(stmt!= null) stmt.close(); if(conn!= null) conn.close(); } catch(Exception se) { se.printStackTrace(); }
java向mysql中插入数据
import java.sql.*; String JDBC_DRIVER ="com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost/test"; // Database credentials String USER = "root"; String PASS = "root"; Connection conn = null; try{ conn = DriverManager. getConnection(DB_URL,USER,PASS); PreparedStatement stat = conn.prepareStatement( "insert into DOWNLOAD_RECORD values(?,?,?,?,?,?,?,?)"); stat.setString(1, "2"); stat.setString(2, "2"); stat.setString(3, "2"); stat.setString(4, "2"); stat.setString(5, "2"); stat.setString(6, "2"); stat.setString(7, "2"); stat.setString(8, "2"); stat.executeUpdate(); conn.close(); } catch(Exception se) { se.printStackTrace(); }
java连接oracle数据库
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.util.Calendar; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Node; import ensemble.dbc.DbConnection; public class fun13_ReceiptSettleDao { private Connection conn = null; public int receiptsettle(String esb2ensemble) { this.conn = DbConnection.getConn (); CallableStatement cs = null; int n = -1; try{ //ESB收到消息后解析报文 Document doc1 = DocumentHelper. parseText(esb2ensemble); //获取叶子节点b Node node1 = doc1.selectSingleNode("ROOT/BODY/SVR_MESSAGE_IN/v1" ); String v1=node1.getText(); System. out.println(v1); Node node3 = doc1.selectSingleNode("ROOT/BODY/SVR_MESSAGE_IN/v2" ); String v3=node3.getText(); System. out.println(v3); Node node4 = doc1.selectSingleNode("ROOT/BODY/SVR_MESSAGE_IN/v3" ); String v4=node4.getText(); System. out.println(v4); Node node5 = doc1.selectSingleNode("ROOT/BODY/SVR_MESSAGE_IN/v4" ); String v5=node5.getText(); System. out.println(v5); int vv5=Integer.parseInt(v5); try { cs = conn.prepareCall("{call sp_receiptsettle(?,?,?,?)}"); cs.setString(1, v1); cs.setString(2, v3); cs.setString(3, v4); cs.setInt(4, vv5); cs.execute(); n = 0; } catch (SQLException e) { e.printStackTrace(); return n; } finally { try { cs.close(); this.conn .close(); } catch (SQLException e) { e.printStackTrace(); } } } catch (DocumentException e) { e.printStackTrace(); } return 0; } } import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbConnection { private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ; private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl" ; private static final String DBUSER = "ENSEMBLE"; private static final String DBPASSWORD = "12345611"; private static Connection conn; public static Connection getConn() { try { Class. forName(DBDRIVER); conn = DriverManager.getConnection( DBURL, DBUSER , DBPASSWORD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn ; } }