First, the Ojdbc6.jar package is imported into the project, and the build path
Two
1. Normal Connection:
String classname= "Oracle.jdbc.driver.OracleDriver";//oracle's ClassName (Ojdbc6.jar)
String url= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//oracle url (ojdbc6.jar)
String classname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//sqlserver's ClassName(Sqljdbc.jar)
String url="jdbc:sqlserver://localhost:1433;databasename= database name";//sqlserver URL(sqljdbc.jar)
Class.forName (className);//Import driver class
Connection conn=drivermanager.getconnection (URL, "sa", "123");//Create a database connection object, three parameters are: Url,user,password
Statement st=conn.createstatement ();
New ========================begin
int Addcount=st.executeupdate ("INSERT into AAA values (' New 4 ', ' Add 4 ', ' Add 4 ')");
if (addcount>0)
System.out.println ("new success");
Else
SYSTEM.OUT.PRINTLN ("New Failure");
New ========================end
Delete ========================begin
int deletecount = st.executeupdate ("delete AAA where \" id\ "= ' new 4 '");
if (deletecount>0)
System.out.println ("delete succeeded");
Else
System.out.println ("delete failed");
Delete ========================end
Modify ========================begin
int updatecount=st.executeupdate ("update AAA Set num = ' Modify 1 ', tag = ' Modify 1 ' WHERE id = ' 3 '");
if (updatecount>0)
SYSTEM.OUT.PRINTLN ("modified successfully");
Else
SYSTEM.OUT.PRINTLN ("modification failed");
Modify ========================end
Query ========================begin
ResultSet rs = st.executequery ("SELECT * from AAA");
SYSTEM.OUT.PRINTLN ("id \ t num \ t tag");
while (Rs.next ()) {
String a=rs.getstring (1);//based on the subscript to take the field value, subscript starting from 1
String b=rs.getstring ("num");//Depending on the field name, the data type followed by Get is also determined by the database, which can be int/double, etc.
String c=rs.getstring (3);
System.out.println (A + "\ t" +b+ "\ T" +c);
}
Query ========================end
Rs.close ();
St.close ();
Conn.close ();
2. Precompiled Connection:
String classname= "Oracle.jdbc.driver.OracleDriver";//oracle's ClassName (Ojdbc6.jar)
String url= "Jdbc:oracle:thin:@127.0.0.1:1521:magma";//oracle url (ojdbc6.jar)
String classname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//sqlserver's ClassName (Sqljdbc.jar)
String url= "jdbc:sqlserver://localhost:1433;databasename= database name";//sqlserver url (sqljdbc.jar)
Class.forName (className);//Import driver class
Connection conn=drivermanager.getconnection (URL, "Jczb", "HXZYJCZB");//Create a database connection object, three parameters are: Url,user,password
New =============================begin
String sql= "INSERT into AAA values (?,?,?)";
PreparedStatement PS = conn.preparestatement (SQL);
Ps.setstring (1, "ID5");//For precompiled SQL, string type, other types can be optional such as: Ps.setint (1, 199); No matter what type of field, as long as the data can be turned over, it can be int/double, etc.
Ps.setint (2, 8);
Ps.setstring (3, "Tag4");
int executeupdate = Ps.executeupdate ();
if (executeupdate>0)
System.out.println ("new success");
Else
SYSTEM.OUT.PRINTLN ("New Failure");
New =============================end
Modify =============================begin
String sql1= "Update AAA set num =?";
PreparedStatement PS1 = conn.preparestatement (SQL1);
Ps1.setstring (1, "4");//For precompiled SQL, string type, other types can be optional such as: Ps.setint (1, 199); Regardless of the type of field, as long as the data can be turned over, it can be int/double, etc.
Ps1.setstring (2, "id1");
int executeUpdate1 = Ps1.executeupdate ();
if (executeupdate1>0)
SYSTEM.OUT.PRINTLN ("modified successfully");
Else
SYSTEM.OUT.PRINTLN ("modification failed");
Modify =============================end
Query =============================begin
String sql2= "SELECT * from AAA";
PreparedStatement PS2 = conn.preparestatement (SQL2);
Precompiled no parameters to pass, can not be passed, string type, other types can be optional such as: Ps.setint (1, 199); No matter what type of field, as long as the data can be turned over, it can be int/double, etc.
ResultSet rs = Ps2.executequery ();
SYSTEM.OUT.PRINTLN ("id \ t num \ tag \ t");
while (Rs.next ()) {
System.out.println (rs.getstring (1) + "\ T" +rs.getint ("num") + "\ T" +rs.getstring (3));
/get the data type behind it is also the database or its own needs, regardless of the type of field, as long as the data can be transferred, can be int/double, etc.
}
Query =============================end
New =============================begin
String sql3= "Delete AAA where id =?";
PreparedStatement ps3 = conn.preparestatement (SQL3);
Ps3.setstring (1, "id2");//For precompiled SQL, string type, other types can be optional such as: Ps.setint (1, 199); No matter what type of field, as long as the data can be turned over, it can be int/double, etc.
int executeUpdate3 = Ps3.executeupdate ();
if (executeupdate3>0)
System.out.println ("delete succeeded");
Else
System.out.println ("delete failed");
New =============================end
Ps3.close ();
Conn.close ();
Database connection (oracle/sqlserver, delete, change, check)