Java JDBC improves program portability

Source: Internet
Author: User
Tags getmessage stmt
Many Java beginners begin to access JDBC programming, both online and in most textbooks.
Introduction to JDBC General programs:
A complete program that can be run
Import java.sql.*;

public class Databasedemo
{
public static void Main (String args[])
{
Connection con;
Statement stmt;
ResultSet rs;

Load the driver class
Try
{//write strings directly inside the program Com.microsoft.jdbc.sqlserver.SQLServerDriver
Reduces the portability of programs.
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch (ClassNotFoundException e)
{
System.out.println (E.getmessage ());
}

Get database connection, statement and the ResultSet
Try
{
Con=drivermanager.getconnection ("Jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs", "sa", "");

Stmt=con.createstatement ();
Rs=stmt.executequery ("SELECT * from authors");

while (Rs.next ())
{
for (int i=1;i<=rs.getmetadata (). getColumnCount (); i++)
{
System.out.print (rs.getstring (i) + "| ");
}
System.out.println ();
}
}catch (SQLException e)
{
System.out.println (E.getmessage ());
}
}

}
This program obviously has a problem, that is, the program portability is very poor, to join me now do not use SQL Server, I will be using Sybase or Orcale
And the program has been big bag, how to do. Here we can like the Uld file in VC ADO. Use our properties file. Writes attributes and corresponding values to the property file. For example, we enter some content in the property file Basic.properties:
Connectionurl:jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs
DriverManager:com.microsoft.jdbc.sqlserver.SQLServerDriver
Userid:sa
Password
Note that there should be a colon between the property and the value, in English mode, not in a Chinese colon.
We can read the value of the file attribute by java.util the properties class inside the package.
Such as
Java.util.Properties pro=new java.util.Properties Pro ();
Try
{
Pro.load (New FileInputStream ("Basic.properties"));
}catch (IOException e) {...}
Consql=pro.getproperty ("Connectionurl");
Driverstr=pro.getproperty ("DriverManager");
Userid=pro.getproperty ("UserId");
Password=pro.getproperty ("password");
So we can get the property values in properties. When our application is going to switch to another database management system, we just
Modifying the properties file is OK.

Improve the above program (can be run)
/*
Aa.java
*/
Import java.util.*;
Import java.io.*;
Import java.sql.*;


public class AA
{
public static void Main (String args[])
{
String Consql;
String Driverstr;
String userId;
String password;
Connection con;
Statement stmt;
ResultSet rs;
Properties Pro=new properties ();
Load the properties file and get the Proterties
Try
{

Pro.load (New FileInputStream ("Basic.properties"));

}catch (IOException e)
{
System.out.println (E.getmessage ());
}

Consql=pro.getproperty ("Connectionurl");
Driverstr=pro.getproperty ("DriverManager");
Userid=pro.getproperty ("UserId");
Password=pro.getproperty ("password");
System.out.println (consql+ "\ n" +driverstr+ "\ n" +userid+ "+password");
Load the driver class
Try
{
Class.forName (DRIVERSTR);
}catch (ClassNotFoundException e)
{
System.out.println (E.getmessage ());
}

Get the database connection
Try
{
Con=drivermanager.getconnection (Consql,userid,password);
String querystr= "SELECT * from authors";
Stmt=con.createstatement ();
Rs=stmt.executequery (QUERYSTR);

while (Rs.next ())
{
for (int i=1;i<=rs.getmetadata (). getColumnCount (); i++)
{
System.out.print (rs.getstring (i) + "| ");
}
System.out.println ();
}
}catch (SQLException e)
{
System.out.println (E.getmessage ());
}

}

}
Note:::: The property file should be placed under the same directory as your Java source file.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.