## page was renamed from JDBC
= JDBC =
== Table metadata sample Servlet ==
Tomcat 7 conf/context.xml datasource example
{{{#!highlight xml
}}}
{{{#!highlight java
package org.allowed.bitarus;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.security.Security;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.allowed.bitarus.IWSTest;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
// http://localhost:8081/WebApp/servletxxx
public class Servletx extends HttpServlet{
@Resource(name="jdbc/mysqlx")
private DataSource ds;
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String text="";
try{
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
InitialContext context = new InitialContext(env);
IWSTest test = (IWSTest) context.lookup("WSTest/remote");
text=test.helloWorld();
}
catch(Exception ex){
}
out.println("
Hello World!");
out.println(String.format("Hello World!!! %s
",text));
try{
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from ost_ticket;");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.println(""+ rs.getString("name") +"
");
}
rs.close();
conn.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
getTableInfo("",out);
out.println("");
}
private String getDataType(int dt,int nullable){
if(dt==93 ) return "Date";
if(dt==12 ) return "String";
if(dt==-7 && nullable==0) return "boolean";
if(dt==-1) return "String";
if(dt==4 && nullable==0) return "int";
if(dt==1 && nullable==0) return "int";
if(dt==-6 && nullable==0) return "int";
if(dt==-7 && nullable==1) return "Boolean";
if(dt==4 && nullable==1) return "Integer";
if(dt==1 && nullable==1) return "Integer";
if(dt==-6 && nullable==1) return "Integer";
return "";
}
private void showColumns(DatabaseMetaData dmd,String table,PrintWriter out) throws SQLException {
ResultSet rs = dmd.getColumns(null,null,table,null);
while(rs.next()){
String column = rs.getString("COLUMN_NAME");
int dataType = rs.getInt("DATA_TYPE");
int nullable = rs.getInt("NULLABLE");
out.println(" private "+ getDataType(dataType,nullable) + " " + column + "; // " +nullable +"
");
}
out.println(" }
");
}
private void getTableInfo(String tableName,PrintWriter out){
try{
Connection conn = ds.getConnection();
DatabaseMetaData metaData = conn.getMetaData();
String[]types={"TABLE"};
ResultSet rs = metaData.getTables(null,null,null,types);
while(rs.next()){
String table = rs.getString("TABLE_NAME");
out.println(" public class "+ table +" {
");
showColumns(metaData,table,out);
}
rs.close();
conn.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}}}
== MySQL connector maven dependency ==
{{{
mysql
mysql-connector-java
5.1.31
}}}
== MySQL get JDBC connection ==
{{{#!highlight java
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "userx","passx");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}}}
== Oracle thin client JDBC connection ==
{{{#!highlight java
Connection conn=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "userx","passx");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}}}
== jdbc code gen project ==
* Check https://code.google.com/p/jdbc-code-gen/