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");
}
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"));
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.