Adding data to a database in Java is successful, but the added data is not found in the database

Source: Internet
Author: User
Tags stmt

The procedure is as follows:
--The first class:
Package com. studentcurriculavariable;
Import java.sql.*;
public class Jdbc_connection {
Static String drivername= "Sun.jdbc.odbc.JdbcOdbcDriver";
Static String url= "Jdbc:odbc:chong";
Static String username= "SA";
Static String password= "123456";
static{
try{
Class.forName (drivername);
SYSTEM.OUT.PRINTLN ("created successfully! ");
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
public static Connection getconnection () {
Connection Conn=null;
try{
Conn=drivermanager.getconnection (Url,username,password);
SYSTEM.OUT.PRINTLN ("Connection database succeeded! ");
}catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}
public static void Free (ResultSet rs,connection conn,statement stmt) {
try{
if (rs!=null)
Rs.close ();
}catch (SQLException e) {
System.out.println ("Shutdown resultset failed! ");
E.printstacktrace ();
}finally{
try{
if (conn!=null)
Conn.close ();
}catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Shutdown Connection failed! ");
E.printstacktrace ();
}finally{
try{
if (stmt!=null)
Stmt.close ();

}catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Shutdown statement failed! ");
E.printstacktrace ();
}
}
}
}

}


--The second class:
Package com. studentcurriculavariable;
public class Uservo {
private static String name;
private static String ID;
private static String sex;
private static int age;
private static String address;
private static String Xuanclass;
private static String;
private static String username;
private static int password;
public static String getaddress () {
return address;
}
public void setaddress (String address) {
this.address=address;
}
public static String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username=username;
}
public static String Getroom () {
return to the hostel;
}
public void Setroom (String) {
This.room=room;
}
public static String Getxuanclass () {
return xuanclass;
}
public void Setxuanclass (String xuanclass) {
This.xuanclass=xuanclass;
}
public static String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex=sex;
}
public static String GetId () {
return ID;
}
public void SetId (String id) {
This.id=id;
}
public static String GetName () {
return name;
}
public void SetName (String name) {
This.name=name;
}
public static int Getage () {
return age;
}
public void Setage (int.) {
This.age=age;
}
public static int GetPassword () {
return password;
}
public void SetPassword (int password) {
This.password=password;
}
}


--A third class:
Package com. studentcurriculavariable;
Import java.sql.*;
public class AddUser {
public void Add (Uservo uservo) {
Connection Conn=null;
PreparedStatement Pstm=null;
ResultSet Rs=null;
try{
Conn=jdbc_connection.getconnection ();
String sql= "INSERT into Stu (Name,id,sex,age,address,xuanclass,room,username,password) VALUES (?,?,?,?,?,?,?,?,?)";
Pstm=conn.preparestatement (SQL);
Pstm.setstring (1,uservo.getname ());
Pstm.setstring (2,uservo.getid ());
Pstm.setstring (3,uservo.getsex ());
Pstm.setint (4,uservo.getage ());
Pstm.setstring (5,uservo.getaddress ());
Pstm.setstring (6,uservo.getxuanclass ());
Pstm.setstring (7,uservo.getroom ());
Pstm.setstring (8,uservo.getusername ());
Pstm.setint (9,uservo.getpassword ());
SYSTEM.OUT.PRINTLN ("Data added successfully! ");
}catch (Exception e) {
E.printstacktrace ();
}finally{
Jdbc_connection.free (RS, Conn, pstm);
}
}
public static void Main (string[] args) {
AddUser adduser=new AddUser ();
Uservo uservo=new Uservo ();
String name= "Zhao Liu";
String id= "00006";
String sex= "Male";
int age=19;
String address= "Haidian District, Beijing";
String xuanclass= "Data structure";
String room= "Software engineering 14-1";
String username= "Zhao Liu";
int password=123;
Uservo.setname (name);
Uservo.setid (ID);
Uservo.setsex (Sex);
Uservo.setage (age);
Uservo.setaddress (address);
Uservo.setxuanclass (Xuanclass);
Uservo.setroom (guest);
Uservo.setusername (username);
Uservo.setpassword (password);
Adduser.add (Uservo);
}
}
The execution of the program shows success, but there is no data in the database, where is the problem?

Adding data to the database in Java succeeds, but the added data is not found in the database

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.