JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package javaDB; import java.sql.*; import com.microsoft.sqlserver.jdbc.*; public class test { public static void main(String[] args) { // Declare the JDBC objects. Connection con = null; CallableStatement cstmt = null; ResultSet rs = null; try { // Establish the connection. SQLServerDataSource ds = new SQLServerDataSource(); //ds.setIntegratedSecurity(true); ds.setServerName("localhost"); ds.setPortNumber(1433); ds.setDatabaseName("WSP_IBE"); ds.setUser("user01"); ds.setPassword("password01"); con = ds.getConnection(); // Execute a stored procedure that returns some data. cstmt = con.prepareCall("{call dbo.uspGetAircraft()}"); //cstmt.setInt(1, 50); rs = cstmt.executeQuery(); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println("aircraft_code: " + rs.getString("aircraft_code")); System.out.println("aircraft_english: " + rs.getString("aircraft_english")); System.out.println(); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch(Exception e) {} if (cstmt != null) try { cstmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } } } |
JSP mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <html> <head> <title>Connection with mysql database</title> </head> <body> <h1>Connection status </h1> <table> <% String connectionURL = "jdbc:mysql://localhost:3306/IBE?characterEncoding=UTF-8&serverTimezone=UTC"; Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "user02", "user99"); String query ="SELECT airline_code,airline_korean FROM IBE.Airlines where airline_code = ?"; //String query ="SELECT airline_code,airline_korean FROM IBE.Airlines"; pstmt = connection.prepareStatement(query); pstmt.setString(1,"OZ"); rs = pstmt.executeQuery(); while(rs.next()){ String airline_code = rs.getString("airline_code"); String airline_korean = rs.getString("airline_korean"); %> <tr> <td width="100"><%=airline_code%></td> <td width="100"><%=airline_korean%></td> </tr> <% } } catch(Exception ex){ out.println(ex.toString()); }finally{ if(rs != null) try{rs.close();}catch(SQLException ex){} if(pstmt != null) try{pstmt.close();}catch(SQLException ex){} if(connection != null) try{connection.close();}catch(SQLException ex){} } %> </table> </body> </html> |
jsp paramerer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.PreparedStatement" %> <%@ page import = "java.sql.ResultSet" %> <%@ page import = "java.sql.SQLException" %> <% String connectionURL = "jdbc:mysql://localhost:3306/IBE?characterEncoding=UTF-8&serverTimezone=UTC"; Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; String code = request.getParameter("code"); //Code if(code == null) code=""; String Decode = code; try{ if(code.length()==2){ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "user02", "user99"); String query ="SELECT airline_code,airline_korean FROM IBE.Airlines where airline_code = ? LIMIT 0, 1"; pstmt = connection.prepareStatement(query); pstmt.setString(1,code); rs = pstmt.executeQuery(); while(rs.next()){ Decode = rs.getString("airline_korean"); } out.println(Decode); }else out.println(code); } catch(Exception ex){ out.println(ex.toString()); }finally{ if(rs != null) try{rs.close();}catch(SQLException ex){} if(pstmt != null) try{pstmt.close();}catch(SQLException ex){} if(connection != null) try{connection.close();}catch(SQLException ex){} } %> |