Java basic methods to connect to the database and execute the query, do you see what's wrong with this?

Source: Internet
Author: User
Tags stringbuffer

package com.yuan.test;import java.io.ioexception;import java.sql.connection;import  java.sql.drivermanager;import java.sql.sqlexception;public class test123 {public  Test123 ()  {}public static void main (String[]args) {User user=new user ("123",   "YUANW",  "man",  "yuanw"); Test123 test123=new test123 (); try {test123.updateuser (user);}  catch  (sqlexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}} Public void updateuser (User user)  throws java.sql.sqlexception { Java.lang.stringbuffer sqlbuf = new java.lang.stringbuffer (); SqlBuf.append ("Update T_ User set user_name= ' "); Sqlbuf.append (User.getusername ()); Sqlbuf.append (" ', "); Sqlbuf.append (" SEX= "); Sqlbuf.append (User.getsex ()), Sqlbuf.append ("',"); Sqlbuf.append ("email="); Sqlbuf.append (User.getemail ()); Sqlbuf.append ("'  WHERE&NBsp;userid= "); Sqlbuf.append (User.getid ()); String sql = sqlbuf.tostring (); SYSTEM.OUT.PRINTLN ("Print update UPDATE statement:" +sql); Java.sql.connection con = getconnectionfromdatasource () ; Java.sql.statement stmt = con.createstatement (); stmt.executeupdate (SQL); Stmt.close (); con.close ();} Private connection getconnectionfromdatasource ()  {String driverName =  " Com.microsoft.sqlserver.jdbc.SQLServerDriver "; //  load JDBC driver string dburl = " JDBC: Sqlserver://localhost:1433; databasename=hibernate "; //  connection server and database teststring username  =  "sa"; //  Default user name string userpwd =  "sa123"; //  password connection dbconn  = null;try {class.forname (drivername);}  catch  (classnotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();} Try {dbconn = drivermanager.getconnection (dburl, username, USERPWD); System.out.println ("connection successful!");  //  If the connection succeeds   console output connection} catch  (sqlexception e)  {// TODO  Auto-generated catch blocke.printstacktrace ();}  successful!return dbconn;}}

Note: The Sqljdbc4.jar package needs to be introduced

Tip: Modify the Table field property values.

ALTER TABLE tablename ALTER COLUMN colname Newdatatype

@@ 兄弟 all help me to see, so write call will have what problem???

Java basic methods to connect to the database and execute the query, do you see what's wrong with this?

Related Article

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.