Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

navigating to database using java

Sat Apr 14, 2012 3:21 am

hai
i like to get code for making adding deleting navigating to database using javaide



Re: good code "for making good fromrs"

Sat May 12, 2012 11:27 pm

Code:

import java
.sql.*;

public 
Connection openConnection() {
  
Properties properties = new Properties();
  
properties.put("user", ...);
  
properties.put("password", ...);
  
properties.put("characterEncoding""ISO-8859-1");
  
properties.put("useUnicode""true");
  
String url "jdbc:mysql://hostname/database";

  Class.
forName("com.mysql.jdbc.Driver").newInstance();
  
Connection c DriverManager.getConnection(urlproperties);
  return 
c;
}
 


To create connection and statement :
Code:
Connection c = createConnection();
Statement st = c.createStatement();

To run SQL
Code:

int id 
1//Any value
String sql "SELECT Name FROM students WHERE Id = " id;
ResultSet rs st.executeQuery(sql);
 


to show results
Code:
ResultSet rs = st.getResultSet();
try {
  while (rs.next()) {
    int id = rs.getInt(1);
    String userName = rs.getString(2);
    String firstName = rs.getString(3);
    String surname = rs.getString(4);
    Timestamp timeReg = rs.getTimestamp(5);
     
  }
} finally {
  rs.close();
}







for net-beans
Code:
http://netbeans.org/kb/docs/ide/java-db.html


Post a reply
  Related Posts  to : navigating to database using java
 Java Library Database Management System Project     -  
 2 different database     -  
 Selecting a Database in php     -  
 information about database     -  
 database connection     -  
 List all database in php     -  
 Login using jsp,Servlets and Database following MVC     -  
 Persist in DataBase with AspectJ (AOP)     -  
 dump Mysql database     -  
 import Mysql database     -