Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JDBC jazzed

  Asked By: Ibthaj    Date: May 29    Category: Java    Views: 515
  

reading a database (SQL SERVER 2000)
--------------------------------------------

import java.sql.*;
import java.util.*;

class DriverApp {
public static void main(String args[]) {
try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Enumeration drivers =
DriverManager.getDrivers();
System.out.println("Available drivers: ");
while (drivers.hasMoreElements()) {
Driver driver =
(Driver)drivers.nextElement();
System.out.println(" Driver:
"+driver.getClass().getName());
System.out.println(" Major version:
"+driver.getMajorVersion());
System.out.println(" Minor version:
"+driver.getMinorVersion());
System.out.println(" JDBC
compliant: "+driver.jdbcCompliant());

DriverPropertyInfo props[] =
driver.getPropertyInfo(" ", null);
if (props !=null) {
System.out.println(" Properties
");
for (int i = 0; i<props.length;
++i) {
System.out.println("
Name: "+props[i].name);
System.out.println("
Description: "+props[i].description);
System.out.println("
Value: "+props[i].value);
if (props[i].choices !=null) {
System.out.println("
Choices: ");
for (int j=0;
j<props[i].choices.length; ++j)
System.out.println("
"+props[i].choices[j]);
}
System.out.println("
Required: "+props[i].required);
}
}
}
} catch(Exception ex) {
System.out.println(ex);
System.exit(0);
}
}
}

_________________________________________________

"hide" is the dsn name
----------------------------------------------
import java.sql.*;
import java.util.*;

class ResultApp {
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:hide");
Statement stat = con.createStatement();
String sql = "select * from authors";
ResultSet result = stat.executeQuery(sql);
displayResults(result);
con.close();
}
catch(Exception e)
{
System.out.println(e);
System.exit(0);
}
}
static void displayResults(ResultSet r) throws
SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int numColumns =rmeta.getColumnCount();
for (int i=1; i<=numColumns; ++i)
{
if (i<numColumns)
System.out.println(rmeta.getColumnName(i)+ " |
");
else
System.out.println(rmeta.getColumnName(i));
}
while(r.next())
{
for(int i=1; i<=numColumns; ++i)
{
if (i<numColumns)
System.out.println(r.getString(i)+" | ");
else
System.out.println(r.getString(i).trim());
}
}
}
}
_______________________________________________

writing out to a file: -
----------------------------------------------
import java.io.*;
public class Dealer
{
String dealerName;
String dealerAddress;
String dealerPhone;
String dealerServices;

InputStreamReader keyreader;
BufferedReader bfreader;
FileOutputStream fswriter;

public Dealer()
{
try
{
keyreader = new InputStreamReader(System.in);
bfreader = new BufferedReader(keyreader);
System.out.println("Dealer Name: ");
dealerName = bfreader.readLine();
System.out.println("Address: ");
dealerAddress = bfreader.readLine();
System.out.println("Phone Number: ");
dealerPhone = bfreader.readLine();
System.out.println("Services Offered: ");
dealerServices = bfreader.readLine();

keyreader.close();
bfreader.close();

fswriter = new FileOutputStream
("D:\\java\\test.txt", true);
String temp = dealerName + ":" +dealerAddress+ ":"
+dealerPhone+":"+dealerServices;
fswriter.write(temp.getBytes());
System.out.println("Finished writing dealer details
to the file");
fswriter.close();
}
catch(FileNotFoundException fn)
{
System.out.println ("The specified file does not
exist");
}
catch(IOException fn)
{
System.out.println("Error performing IO
operation");
}
}
public static void main(String args[])
{
Dealer obj = new Dealer();
}
}

-------------------------------------------------

writing to a database

-----------------------------------------------

import java.util.*;
import java.sql.*;
public class test
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:hide","sa","");
PreparedStatement st = con.prepareStatement("insert
login values (?,?,?,?,?)");
st.setString(1,login);
st.setString(2,pass);
st.setString(3,email);
st.setString(4,address);
st.setString(5,phone);
int r=st.executeUpdate();
if (r!=0)
out.println("Successfully registered");
else
out.println("Error in registration");
}
catch(Exception e)
{
out.println(e);
}
}
-----------------------------

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on JDBC jazzed Or get search suggestion and latest updates.




Tagged: