Java using JDBC Operations Database sample sharing _java

Source: Internet
Author: User

Package DAO;

Import java.sql.*;

public class Basedao {

Oracle
private static final String dirver= "Oracle.jdbc.driver.OracleDriver";
private static final String url= "Jdbc:oracle:thin: @localhost: 1521:xe";
private static final String name= "admin";
private static final String pass= "123";

Sql server
private static final String dirver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url= "Jdbc:sqlserver://localhost:1433;databasename=tbldb";
private static final String name= "sa";
private static final String pass= "sa";

Mysql
private static final String dirver= "Com.mysql.jdbc.Driver";
private static final String url= "Jdbc:mysql://127.0.0.1:3306/school";
private static final String name= "root";
private static final String pass= "123456";

Public Connection getcconnection ()
{
Connection Conn=null;
try {
Class.forName (Dirver);
Conn=drivermanager.getconnection (Url,name,pass);
catch (Exception e) {
E.printstacktrace ();
}
Return conn;
}
public void Closall (Connection conn,preparedstatement ps,resultset rs)
{
try {
if (NULL!=RS)
Rs.close ();
if (NULL!=PS)
Ps.close ();
if (null!=conn)
Conn.close ();
catch (Exception e) {
E.printstacktrace ();
}

}

public int executeupdate (String sql,string parms[])
{
Connection Conn=null;
PreparedStatement Ps=null;
int result=-1;
try {
Conn=getcconnection ();
Ps=conn.preparestatement (SQL);
if (null!=parms) {
for (int i=0;i<parms.length;i++) {
Ps.setstring (I+1,parms[i]);
}
}
Result=ps.executeupdate ();
Execute ();

catch (Exception e) {
System.out.println (E.getmessage ());
}finally{
Closall (conn, PS, NULL);
}
return result;
}
public static void Main (string[] args) throws Exception
{
Basedao b=new Basedao ();
Connection con=b.getcconnection ();
System.out.println (con.isclosed ());
}
}

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.