JDBC Connection Database preparestatement__ database

Source: Internet
Author: User
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException; Import Team.


Jdbctest;
	 public class Dbcstudent {//define database access parameter String URL = "Jdbc:sqlserver://localhost:1433;databasename=jdbctest";
	 String user = "sa";
	 String password = "AAAAAA";
	 static String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
	 Connection Conn;
	 PreparedStatement PS;
	 ResultSet rs;
	 String id= "";
	 String name= "";
	 int age=0;
	 String sex= "";
		1, load driver static {try {class.forname (drivername);
		catch (ClassNotFoundException e) {System.out.println ("Drive load Failed"); }//2, create Connection object public Connection getconnection () throws sqlexception{Conn=drivermanager.getconnection (Url,user,pas
		Sword);
	Return conn;
	} public void Add (String id,string name,int age,string Sex) throws ClassNotFoundException, SQLException {//define SQL statement String sql= INSERT into Studentmanage (studentid,studentnAme,age,sex) VALUES (?,?,?,?) ";
    3, create the Statement object Ps=getconnection (). preparestatement (SQL);
    Ps.setstring (1,id);
    Ps.setstring (2,name);
    Ps.setint (3,age);
    Ps.setstring (4,sex);
   Ps.executeupdate ();
    4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
Getconnection (). Close (); }/*public void Update (String id,string name,int age,string Sex) throws ClassNotFoundException, SQLException {//Set Semantic SQL statement String sql= "UPDATE studentmanage SET studentname=?,age=?,sex=?"
	    where studentid=? ";
	     3, create the Statement object Ps=getconnection (). preparestatement (SQL);
	     Ps.setstring (1,name);
	     Ps.setint (2,age);
	     Ps.setstring (3,sex);
	     Ps.setstring (4,id);
	   Ps.executeupdate ();
	     4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
	Getconnection (). Close (); public void Delete (String id) throws ClassNotFoundException, SQLException {//define SQL statement string sql= "Delete S
	    Tudentmanage WHERE studentid=? "; 3, create the statement object Ps=getconnectIon (). preparestatement (SQL);
	   Ps.setstring (1,id);
	     4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
	Getconnection (). Close (); }*/Public String get (string id) throws ClassNotFoundException, SQLException {//define SQL statement string sql= "select S
	    Tudentid,studentname,age,sex from Studentmanage WHERE studentid=? ";
	     3, create the Statement object Ps=getconnection (). preparestatement (SQL);
	     Ps.setstring (1,id);
	     ResultSet rs = Ps.executequery ();
	    	 while (Rs.next ()) {id=rs.getstring ("StudentID");
	    	 Name=rs.getstring ("Studentname");
	    	 Age=rs.getint ("Age");
	     Sex=rs.getstring ("Sex");
	     //4, traverse result set: Insert record here does not need//5, Close Resource object Ps.close ();
	     Getconnection (). Close ();
	Return "School Number:" +id+ "Name:" +name+ "Age:" +age+ "Sex:" +sex; public void Get () throws ClassNotFoundException, SQLException {} public static void Main (string[) args) throws Cla
     ssnotfoundexception,sqlexception {//jdbctest jt=new jdbctest ();
    Jt.add (); //jt.update ();
Jt.delete ();

 }

}

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.