Monday 15 June 2015

Connectivity Pseudocode

Connectivity pseudocode:
1. Load JDBC driver
2. Specify the name and location of the database being used.
3. Connect to the data base with Connection object.
4. Execute query.
5. Get the result in ResultSet object.
6. Finish by closing the ResultSet statments and Connection objects.

Example:
class.forName("sun.jdbc.odbc.JdbsOdbcDriver");
url=name of database
Connection conn=DriverManager.getConection(url);
Statement st=conn.CreateStatement();
ResultSet rs=st.executeQuery();   //executeUpdate is also used for other queries
st.close();

PreparedStatement obj= conn.prepareStatement(str);
obj.setInt(1,2)
obj.setString('hey') //passing of parameters

url="jdbc:mysql://localhost:3306/databasename";

DriveManager.getConnection(url, username, password);

String query="select *from tablename";

rs.executeQuery(query);

while(rs.next())
{
System.out.print(rs.getString(1)) //first colun for every row and its type has to be string in this case
}

Enclose everything in a try catch block.

No comments:

Post a Comment