Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Java.sql' - 8 code snippet(s) found

 Sample 1. Database Api using java.sql classes

package BuggyBread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseAPI {

   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "";

   // Database credentials
   static final String USER = "";
   static final String PASS = "";

   Connection conn = null;
   
   public void initializeConnection(){
      try {
         // STEP 2: Register JDBC driver
         Class.forName("com.mysql.jdbc.Driver");

         // STEP 3: Open a connection
         System.out.println("Connecting to database...");
         conn = DriverManager.getConnection(DB_URL, USER, PASS);

         conn.close();
      } catch (SQLException se) {
         // Handle errors for JDBC
         se.printStackTrace();
      } catch (Exception e) {
         // Handle errors for Class.forName
         e.printStackTrace();
      } finally {
         
         try {
            if (conn != null)
               conn.close();
         } catch (SQLException se) {
            se.printStackTrace();
         }// end finally try
      }// end try
   
   }
}

   Like      Feedback     database api  java.sql


 Sample 2. Code Sample / Example / Snippet of java.sql.Connection

  public String prepareBindExecute(HrConnection state) throws SQLException {

Connection con = state.con;

Statement st = null;

ResultSet rs = null;

String ename = null;

try {

final PreparedStatement ps =

con.prepareStatement("select name from emps where empid = ?");

st = ps;

ps.setInt(1, state.id);

rs = ps.executeQuery();

rs.next();

ename = rs.getString(1);

} finally {

close(rs, st);

}

return ename;

}


   Like      Feedback      java.sql.Connection


 Sample 3. Code Sample / Example / Snippet of java.sql.ResultSet

  public String prepareBindExecute(HrConnection state) throws SQLException {

Connection con = state.con;

Statement st = null;

ResultSet rs = null;

String ename = null;

try {

final PreparedStatement ps =

con.prepareStatement("select name from emps where empid = ?");

st = ps;

ps.setInt(1, state.id);

rs = ps.executeQuery();

rs.next();

ename = rs.getString(1);

} finally {

close(rs, st);

}

return ename;

}


   Like      Feedback      java.sql.ResultSet


 Sample 4. Code Sample / Example / Snippet of java.sql.Statement

  public ForStatement(List<DeclarationStatement> declarations,

Expression condition, Expression post, Statement body) {

super(ExpressionType.For, Void.TYPE);

assert declarations != null;

assert body != null;

this.declarations = declarations; // may be empty, not null

this.condition = condition; // may be null

this.post = post; // may be null

this.body = body; // may be empty block, not null

}



@Override public ForStatement accept(Visitor visitor) {

visitor = visitor.preVisit(this);

List<DeclarationStatement> decls1 =

Expressions.acceptDeclarations(declarations, visitor);

final Expression condition1 =

condition == null ? null : condition.accept(visitor);

final Expression post1 = post == null ? null : post.accept(visitor);

final Statement body1 = body.accept(visitor);

return visitor.visit(this, decls1, condition1, post1, body1);

}


   Like      Feedback      java.sql.Statement


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of java.sql.PreparedStatement

  public String prepareBindExecute(HrConnection state) throws SQLException {

Connection con = state.con;

Statement st = null;

ResultSet rs = null;

String ename = null;

try {

final PreparedStatement ps =

con.prepareStatement("select name from emps where empid = ?");

st = ps;

ps.setInt(1, state.id);

rs = ps.executeQuery();

rs.next();

ename = rs.getString(1);

} finally {

close(rs, st);

}

return ename;

}


   Like      Feedback      java.sql.PreparedStatement


 Sample 6. Code Sample / Example / Snippet of java.sql.ResultSetMetaData

  private void output(ResultSet resultSet, PrintStream out)

throws SQLException {

final ResultSetMetaData metaData = resultSet.getMetaData();

final int columnCount = metaData.getColumnCount();

while (resultSet.next()) {

for (int i = 1;; i++) {

out.print(resultSet.getString(i));

if (i < columnCount) {

out.print(", ");

} else {

out.println();

break;

}

}

}

}


   Like      Feedback      java.sql.ResultSetMetaData


 Sample 7. Code Sample / Example / Snippet of java.sql.Array

  private void dumpColumn(ResultSet resultSet, int i) throws SQLException {

final int t = resultSet.getMetaData().getColumnType(i);

switch (t) {

case Types.ARRAY:

final Array array = resultSet.getArray(i);

writer.print("{");

dump(array.getResultSet(), false);

writer.print("}");

return;

case Types.REAL:

writer.print(resultSet.getString(i));

writer.print("F");

return;

default:

writer.print(resultSet.getString(i));

}

}


   Like      Feedback      java.sql.Array


 Sample 8. Code Sample / Example / Snippet of java.sql.DatabaseMetaData

  private String mm(int majorVersion, int minorVersion) {

return majorVersion + "." + minorVersion;

}



@Test public void testMetaDataColumns()

throws ClassNotFoundException, SQLException {

Connection connection = CalciteAssert

.that(CalciteAssert.Config.REGULAR).connect();

DatabaseMetaData metaData = connection.getMetaData();

ResultSet resultSet = metaData.getColumns(null, null, null, null);

assertTrue(resultSet.next()); // there's something

String name = resultSet.getString(4);

int type = resultSet.getInt(5);

String typeName = resultSet.getString(6);

int columnSize = resultSet.getInt(7);

int decimalDigits = resultSet.getInt(9);

int numPrecRadix = resultSet.getInt(10);

int charOctetLength = resultSet.getInt(16);

String isNullable = resultSet.getString(18);

resultSet.close();

connection.close();

}


   Like      Feedback      java.sql.DatabaseMetaData



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus